課程
/后端開發
/Java
/Java入門第三季
我在MyEclipse里面調用String s1=input.next();當輸入:語文+空格+回車;得到的s1確是:yu語文;不知道怎么回事呀?好像有的時候能得到正確結果
2017-03-25
源自:Java入門第三季 1-5
正在回答
會不會是你輸入法問題,空格直接把輸入英文也算進去了,換個輸入法試試?
這段代碼雖然很多不規范的地方,但我在eclipse上運行沒什么問題
慕函數1733451 提問者
package?模擬借書系統;?????//包名一般用英文字母小寫,倒寫域名加模塊名加功能名,用"."分割成四級 import?java.util.Scanner; import?java.util.InputMismatchException;? public?class?rendBook?{?????//類名命名遵從大駝峰原則,首字母要大寫,應為RendBook ? ????public?static?void?main(String[]?args)?throws?Exception?{????//不是很明白為什么要在這里拋異常 ????????//?TODO?Auto-generated?method?stub ???????String[]?book={"語文","數學","英語","政治","歷史","地理"}; ??????? ???????boolean?x=true;??????//見第51行 ???????do{ ???????????Scanner?input=new?Scanner(System.in);????????//建議把input放到循環外面,并在循環結束后關閉input,input.closed() ???????????System.out.println("請輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書"); ???????????try{ ???????????????int?order=input.nextInt();? ???????????????if(order!=1&&order!=2) ???????????????????throw?new?Exception("請按提示輸入數字");?????//括號內不必寫入內容 ???????????????if(order==1){ ???????????????????System.out.println("請輸入圖書名稱:"); ???????????????????String?s1=input.next(); ???????????????????System.out.println(s1); ???????????????????for(int?i=0;i<book.length;i++){ ???????????????????????if(book[i].equals(s1)){ ???????????????????????????System.out.println("有這本書:"+book[i]); ???????????????????????????break; ???????????????????????} ???????????????????????if(i==book.length-1) ???????????????????????????System.out.println("圖書不存在!"); ???????????????????} ???????????????????? ???????????????} ???????????????else{ ???????????????????System.out.println("請輸入圖書序號:"); ???????????????????int?num=input.nextInt(); ???????????????????if(num>book.length){ ???????????????????????System.out.println("圖書不存在!");???? ???????????????????} ???????????????????else{ ???????????????????????System.out.println("有這本書:"+book[num-1]); ???????????????????} ???????????????} ?????????????break;?? ???????????} ???????????catch(InputMismatchException?e){ ????????????System.out.println("輸入有誤,請重新輸入!");?????? ????????????//input.next(); ???????????}? ???????????catch(Exception?e){ ???????????????System.out.println("請按提示輸入數字,謝謝合作!"); ???????????????} ???????}while(x);???????//while(true)就行,第10行可以省略 ?????????? ???????} ? } //還有一些空格縮進問題,寫完最好ctrl+shift+f格式化一下
在eclipse控制臺輸入一般換行輸入
https://yq.aliyun.com/articles/69327
這是阿里的編程規范,你可以下載參考下
package?模擬借書系統; import?java.util.Scanner; import?java.util.InputMismatchException;? public?class?rendBook?{ public?static?void?main(String[]?args)?throws?Exception?{ //?TODO?Auto-generated?method?stub ???????String[]?book={"語文","數學","英語","政治","歷史","地理"}; ?????? ???????boolean?x=true; ???????do{ ???? ???Scanner?input=new?Scanner(System.in); ???? ???System.out.println("請輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書"); ???? ???try{ ???? ???int?order=input.nextInt();? ???? ???if(order!=1&&order!=2) ???? ???throw?new?Exception("請按提示輸入數字"); ???? ???if(order==1){ ???? ???System.out.println("請輸入圖書名稱:"); ???? ???String?s1=input.next(); ???? ???System.out.println(s1); ???? ???for(int?i=0;i<book.length;i++){ ???? ???if(book[i].equals(s1)){ ???? ???System.out.println("有這本書:"+book[i]); ???? ???break; ???? ???} ???? ???if(i==book.length-1) ???????? ???System.out.println("圖書不存在!"); ???? ???} ???? ??? ???? ???} ???? ???else{ ???? ???System.out.println("請輸入圖書序號:"); ???? ???int?num=input.nextInt(); ???? ???if(num>book.length){ ???? ???System.out.println("圖書不存在!"); ??? ???? ???} ???? ???else{ ???? ???System.out.println("有這本書:"+book[num-1]); ???? ???} ???? ???} ???? ?break;?? ???? ???} ???? ???catch(InputMismatchException?e){ ???? System.out.println("輸入有誤,請重新輸入!"); ???? //input.next(); ???? ???}? ???? ???catch(Exception?e){ ???? ???System.out.println("請按提示輸入數字,謝謝合作!"); ???? ???} ???????}while(x); ????????? ???????} }
代碼截圖
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
3 回答關于輸入的問題
2 回答關于輸入和輸出的問題,想不通
2 回答關于輸入判斷問題,請指導
3 回答關于ID輸入非整數異常處理問題
2 回答輸入方法問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-04-06
會不會是你輸入法問題,空格直接把輸入英文也算進去了,換個輸入法試試?
這段代碼雖然很多不規范的地方,但我在eclipse上運行沒什么問題
2017-04-12
在eclipse控制臺輸入一般換行輸入
https://yq.aliyun.com/articles/69327
這是阿里的編程規范,你可以下載參考下
2017-03-25
2017-03-25
代碼截圖