大神看看代碼
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-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
int command;
try{
command = Input.nextInt();
switch(command){
case 1 :
System.out.println("輸入圖書(shū)名稱:");
BookName book = new BookName();
book.judgeName();
break;
case 2 :
? ? ? System.out.println("輸入圖書(shū)序號(hào):");
? BookNumber book1 = new BookNumber();
? book1.judgeNumber();
break;
case -1 :
System.out.println("你的輸入有誤!??!");
continue;
default :
System.out.println("你的輸入有誤請(qǐng)按照要求輸入?。。?);
continue;
}
? ?}catch(Exception e){
System.out.println(e.getMessage());
}
}
? ? }
}
package p2;
import java.util.Scanner;
public class BookNumber {
String books[] = {"C語(yǔ)言", "數(shù)據(jù)結(jié)構(gòu)", "匯編語(yǔ)言", "高數(shù)", "大學(xué)語(yǔ)文", "毛概"};
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("圖書(shū)不存在!!!");
}
}
package p2;
import java.util.*;
public class BookName ?{
String books[] = {"C語(yǔ)言", "數(shù)據(jù)結(jié)構(gòu)", "匯編語(yǔ)言", "高數(shù)", "大學(xué)語(yǔ)文", "毛概"};
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("圖書(shū)不存在?。。?!");
? ? }
}
? ? ??
我有三個(gè)疑問(wèn),第一個(gè)是為什么輸入書(shū)名查找不到???
第二個(gè)是怎樣才能找到書(shū)之后不執(zhí)行throw Exception()語(yǔ)句 ? 用標(biāo)志變量????
第三個(gè)是如何找到書(shū)之后結(jié)束程序???
2016-06-04
1、第一眼看出的問(wèn)題:兩個(gè)查找函數(shù)的for()后面都少了{(lán)。
2、用一個(gè)標(biāo)志變量表示是否找到書(shū)了
3、找到書(shū)之后return就好