大神看看代碼
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("圖書不存在?。。?!");
? ? }
}
? ? ??
我有三個疑問,第一個是為什么輸入書名查找不到???
第二個是怎樣才能找到書之后不執(zhí)行throw Exception()語句 ? 用標志變量????
第三個是如何找到書之后結束程序???
2016-06-04
1、第一眼看出的問題:兩個查找函數的for()后面都少了{。
2、用一個標志變量表示是否找到書了
3、找到書之后return就好