package p2;
import java.util.*;
public class Bookmanager{
public static void main(String [] args){
Scanner Input = new Scanner(System.in);
while(true){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
int command;
try{
command = Input.nextInt();
switch(command){
case 1 :
System.out.println("输入图书名称:");
BookName book = new BookName();
book.judgeName();
break;
case 2 :
System.out.println("输入图书序号:");
BookNumber book1 = new BookNumber();
book1.judgeNumber();
break;
case -1 :
System.out.println("你的输入有误!!!");
continue;
default :
System.out.println("你的输入有误请按照要求输入!!!");
continue;
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
package p2;
import java.util.Scanner;
public class BookNumber {
String books[] = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
Scanner Input = new Scanner(System.in);
int number = Input.nextInt();
public void judgeNumber()throws Exception{
for(int i = 0;i < books.length;i++)
if(i==number)
{
System.out.println("book:"+books[i]);
}
throw new Exception("图书不存在!!!");
}
}
package p2;
import java.util.*;
public class BookName {
String books[] = {"C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概"};
Scanner Input = new Scanner(System.in);
String name = Input.next();
public void judgeName() throws Exception{
for(String name : books)
if(name.equals(books))
System.out.println("book:"+name);
throw new Exception("图书不存在!!!!");
}
}
我有三个疑问,第一个是为什么输入书名查找不到???
第二个是怎样才能找到书之后不执行throw Exception()语句 用标志变量????
第三个是如何找到书之后结束程序???
1、第一眼看出的问题:两个查找函数的for()后面都少了{。
2、用一个标志变量表示是否找到书了
3、找到书之后return就好