qq_什么亡魂溺海沒(méi)有跳海實(shí)在_03283094
2016-11-27 22:36:54
package rentbook;import java.util.Scanner;public class text {?? public? static Scanner scan=new Scanner(System.in);?? public? static book[] bo={?? ??? ??? ?new book("高數(shù)"),?? ??? ??? ?new book("物理"),?? ??? ??? ?new book("語(yǔ)文"),?? ??? ??? ?new book("英語(yǔ)"),?? ??? ??? ?new book("c語(yǔ)言")?? ???? };?? ??? ?public static void main(String[] args) {?? ??? ?// TODO Auto-generated method stub????? while (true ){?? ??? ?? System.out.println("輸入命令:1-按照名稱(chēng)查找圖書(shū);2-按照序號(hào)查找圖書(shū)");????????? int inputnum=inputnum1();????????? switch(inputnum){????????? case 1 : System.out.println("按照名稱(chēng)查找圖書(shū)");?????????????????? inputname();?????????????????? break;????????? case 2: System.out.println("按照序號(hào)查找圖書(shū)");????????????????? System.out.println("請(qǐng)輸入序號(hào)"); ???????? ??? ??? ??? ?try{???????? ??? ??? ??? ??? ?int putnum=inputnum1();???????? ??? ??? ??????? System.out.println(bo[putnum-1]);???????? ??? ??? ??? ?}catch(Exception e){???????? ??? ??? ??? ??? ?scan=new Scanner(System.in);???????? ??? ??? ??? ??? ?System.out.println("您輸入的數(shù)字過(guò)大");???????? ??? ??? ??? ?}???????? ??? ??? ??? ?break;????????? case -1 : System.out.println("請(qǐng)輸入數(shù)字");break;????????? default: System.out.println("輸入的數(shù)字不存在");continue;????????? }????? }?? ?}?? ?private static void inputname() {?? ??? ?// TODO Auto-generated method stub?? ??? ?System.out.println("請(qǐng)輸入書(shū)名");?? ??? ?String input=scan.nextLine();?? ??? ?try{?? ??? ??? ?for(int i=0;i<bo.length;i++){?? ??? ??? ??? ?if(input.equals(bo[i].name))?? ??? ??? ??? ?System.out.println(bo[i].name);?? ??? ??? ?}?? ??? ?}catch(Exception e){?? ??? ??? ?scan=new Scanner(System.in);?? ??? ??? ?System.out.println("請(qǐng)輸入正確的書(shū)名");?? ??? ?}}?? ?private static int inputnum1() {?? ??? ?// TODO Auto-generated method stub?? ??? ?int a=scan.nextInt();?? ??? ?try{?? ??? ??? ?return a;?? ??? ?}catch(Exception e){?? ??? ??? ?scan=new Scanner(System.in);?? ??? ???? return -1;?? ??? ?}?? ?}}
3 回答
已采納

慕粉3291149
TA貢獻(xiàn)71條經(jīng)驗(yàn) 獲得超52個(gè)贊
這種問(wèn)題其實(shí)我也遇見(jiàn)過(guò),你把String input = scan.nextLine(); 改成String input = scan.next(); 就可以了
具體原因網(wǎng)上搜了也沒(méi)搞太明白,大致是 nextLine()必須首先使用scan,否則的話一旦有過(guò)輸入(你前面有過(guò)輸入命令),
再到這里進(jìn)行nextLine()輸入的話,程序就會(huì)直接停止運(yùn)行
添加回答
舉報(bào)
0/150
提交
取消