package?com.imooc.test;
import?java.util.InputMismatchException;
import?java.util.Scanner;
public?class?Exercise?{
private?static?String[]?books?=?{"Java編程思想",?"C語(yǔ)言程序設(shè)計(jì)",?"微積分",?"數(shù)據(jù)結(jié)構(gòu)",?"計(jì)算機(jī)科學(xué)導(dǎo)論"};
public?static?void?main(String[]?args)?{
???????Exercise?exercise?=?new?Exercise();
???????exercise.method();
}
public?void?method()?{
Scanner?in?=?new?Scanner(System.in);
System.out.println("輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)");
try?{
int?index?=?in.nextInt();
switch?(index)?{
??case?1?:
??System.out.println("輸入圖書(shū)名稱:");
??String?name?=?in.next();
??for?(?String?s?:?Exercise.books)?{
??if?(?s.equals(name)?)?{
??System.out.println("book:"+name);
??return;
??}
??}
??System.out.println("圖書(shū)不存在!");
??method();
??break;
??case?2:
??System.out.println("輸入圖書(shū)序號(hào):");
??try?{
??int?num?=?in.nextInt();?
??for?(?String?s?:?Exercise.books)?{
??if?(?s.equals(Exercise.books[num-1]))?{
??System.out.println("book:"+Exercise.books[num-1]);
??return;
??}
??}
??System.out.println("圖書(shū)不存在!");
??method();
??}?catch?(?Exception?e?)?{
??System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
??method();?
??}
??break;
}
}?catch?(?InputMismatchException?input?)?{
System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
method();
}
}
}
2015-08-01
第26行代碼for循環(huán)里的是什么意識(shí)啊,求解釋
2015-07-23
恩,不錯(cuò)不錯(cuò)
可以將switch抽取出來(lái)成為一個(gè)單獨(dú)的方法,哈哈哈
2015-07-22
圖書(shū)圖書(shū)序號(hào)就是其索引號(hào)+1,沒(méi)設(shè)計(jì)Book類,肯定是不夠的,但運(yùn)行足以滿足題目需求。