package?com.imooc_1;
import?java.util.Scanner;
public?class?BorrowBooks?{
public?static?Scanner?sc?=?new?Scanner(System.in);
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
String[]?name?=?{"數據結構","大學英語","高數","網絡工程","軟件工程","毛概"};
while(true){
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
String?book;
try{
int?input?=?print();
switch(input){
case?1:
book?=?getBookByName(name);
System.out.println("book:"+book);
break;
case?2:
book?=?getBookByNum(name);
System.out.println("book:"+book);
break;
case?-1:
System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
sc?=?new?Scanner(System.in);
continue;
default:
System.out.println("命令輸入錯誤!");
continue;
}
break;
}catch(Exception?e){
System.out.println(e.getMessage());
continue;
}
}
}
//按照圖書名稱查找
public?static?String?getBookByName(String[]?nameBooks)?throws?Exception{
System.out.println("請輸入圖書名稱:");
String?nameSearch?=?sc.next();
for(int?i=0;?i<nameBooks.length;?i++){
if(nameSearch.equals(nameBooks[i]))
return?nameBooks[i];
}
throw?new?Exception("圖書不存在!");
}
//按照圖書序號查找
public?static?String?getBookByNum(String[]?namebooks)?throws?Exception{
while(true){
System.out.println("請輸入圖書序號:");
int?num?=?print();
try{
if(num?==?-1){
System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");
continue;
}
String?book?=?namebooks[num-1];
return?book;
}catch(Exception?e){
return?"圖書不存在";
}
}
}
//輸入命令,輸入圖書序號
public?static?int?print(){
int?x;
try?{
x?=?sc.nextInt();
return?x;
}?catch?(Exception?e)?{
//?TODO:?handle?exception
return?-1;
}
}
}
2017-12-08
我也是這種情況,必須把鼠標移到下一行才行
2016-04-18
我記得輸入中文容易出問題,最好是把要輸入的字復制過去。
2016-04-16
雖然不知道什么問題,但是輸入時光標移到下一行輸入就能正確顯示了。