求教我這段代碼為啥每次成功執(zhí)行case=1之后還是會(huì)輸出,break沒起作用呢
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] books ={"高數(shù)","語文 ","數(shù)學(xué)","政治","地理","匯編語言","C語言","遙感"};
int index;?
while (true){
System.out.println("輸入命令行:1-按書名查找;2-按序號查找");
String book;
//此時(shí)需要返回獲取到的輸入的值,若返回的值為整型則執(zhí)行判斷
try{index = Book.input();
//注意switch的用法
switch(index) {
case 1:
System.out.println("請輸入圖書名:");
//此時(shí)需要編寫按書名查找的方法,并返回書名
book = Book.bookname();
System.out.println("book:"+ book);
break;
case 2:
book = Book.numbook();
System.out.println("book:"+ book);
break;
default:
System.out.println("第一次輸入的整型輸入有誤,請重新開始。");
continue;
}
}catch (Exception bne){
//此處異常是由于圖書不存在拋出來的
System.out.println(bne.getMessage());//獲得拋出異常的文字信息
continue;//若檢測到圖書不存在這個(gè)異常,處理為重新執(zhí)行While循環(huán)
}
}
}
2017-09-13
while (true){......}
break 只跳出了switch,不能跳出while死循環(huán)。
所以“System.out.println("輸入命令行:1-按書名查找;2-按序號查找");”仍然執(zhí)行!
2017-09-13
把default放在最后試試