供大家參考
package com.class3;
public class FindBook {
public int id;
public String name;
public FindBook(int id,String name) {
// TODO Auto-generated constructor stub
this.id=id;
this.name=name;
}
public String GetBook() {
// TODO Auto-generated constructor stub
return this.id+"\t"+this.name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.class3;
import java.util.InputMismatchException;
import java.util.Scanner;
public class InitialBook {
FindBook fb[]= {new FindBook(1,"論語(yǔ)"),new FindBook(2,"數(shù)學(xué)")};
public static void main(String[] args) {
InitialBook in=new InitialBook();
in.menu1();
}
public void menu1() {
System.out.println("歡迎使用圖書(shū)查詢小程序,書(shū)籍清單如下:");
System.out.println("圖書(shū)序號(hào)\t書(shū)名");
for(int i=0;i<fb.length;i++) {
System.out.println(fb[i].GetBook());
}
System.out.println("輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
switch(input) {
case 1: menu2();
break;
case 2: menu3();
break;
default:break;
}
sc.close();
}
public void menu2() {
Scanner sc=new Scanner(System.in);
System.out.println("請(qǐng)輸入書(shū)名查找:");
String input=sc.nextLine();
boolean flag=false;
for(int i=0;i<fb.length;i++) {
if(input.equals(fb[i].getName())) {
System.out.println("根據(jù)名稱搜索書(shū)名為:"+fb[i].getName());
flag=true;
break;
}
}
if(flag==false) {
System.out.println("書(shū)名不存在,請(qǐng)重新輸入");
menu2();
}
sc.close();
}
public void menu3() {
try {
Scanner sc=new Scanner(System.in);
System.out.println("請(qǐng)輸入圖書(shū)序號(hào)查找:");
int input=sc.nextInt();
boolean flag=false;
for(int j=0;j<fb.length;j++) {
if(input==fb[j].getId()) {
System.out.println("根據(jù)序號(hào)搜索書(shū)名為:"+fb[j].getName());
flag=true;
break;
}
}
if(flag==false) {
System.out.println("書(shū)名不存在,請(qǐng)重新輸入");
menu3();
}
sc.close();
}catch(InputMismatchException e) {
System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令");
menu3();
}
}
}
2020-02-19
兄弟,你那個(gè)sc.close();是什么意思?
2019-11-28
感謝!