課程
/后端開發(fā)
/Java
/Java入門第三季
這個(gè)finally只執(zhí)行了 第一個(gè)號(hào)System.out.println("是否繼續(xù)查找:Y是,N否");
之后不能進(jìn)行用戶輸入?
問下各位怎么解決啊
2016-08-04
源自:Java入門第三季 1-9
正在回答
沒見過在finally里這么干的,finally一般是用來釋放資源的,不會(huì)在里面做一些等待用戶輸入的操作。
ivwv 提問者
ivwv 提問者 回復(fù) ivwv 提問者
ziom 回復(fù) ivwv 提問者
ivwv 提問者 回復(fù) ziom
import?java.util.Scanner; public?class?Test1?extends?Test2?{ book?b?=?new?book(); public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub Test1?t?=new?Test1(); t.show(); } //用show方法通過if判斷調(diào)用show1和show2方法? public?void?show(){ Scanner?sc?=?new?Scanner(System.in); try?{ b.books(); int?i?=?sc.nextInt(); if?(i==1){ super.show1(); }else?if(i?==?2){ super.show2(); }else{ b.sys3(); } }catch(Exception?e){ b.sys3(); }finally{ //通過finally塊來判斷用戶接下來的操作 System.out.println("是否繼續(xù)查找:Y是,N否"); Scanner?ccc?=new?Scanner(System.in);???? //關(guān)于之前問的finally塊執(zhí)行不完全的,解決辦法:可以通過在外面加一個(gè)while循環(huán)?就會(huì)繼續(xù)執(zhí)行 //也可以新new一個(gè)Scanner?然后也會(huì)繼續(xù)執(zhí)行 //目前這個(gè)是之前在Tese1類里new一個(gè)Scanner,所以要是只通過在finally塊里新new一個(gè)Scanner的話 //會(huì)把之前的sc.next()略過,繼續(xù)執(zhí)行到下一個(gè)ccc.nextLine() //不過用用while循環(huán)里面在新new一個(gè)Scanner?和在show方法里也新new一個(gè)Scanner?就可以解決了 //目前我只知道這么多?沒說到的希望大神們能繼續(xù)說全?謝謝啦??! while(true){ ?????????????????????????? String?yes?=?ccc.nextLine(); if?(yes.equals("y")||yes.equals("Y")){ show(); break; }else?if?(yes.equalsIgnoreCase("N")||yes.equals("n")){ System.out.println("感謝使用!"); break; }else?{ b.sys3(); continue; } } } } }
import?java.util.Scanner; //用show1方法來儲(chǔ)存根據(jù)編號(hào)搜索圖書的程序 public?class?Test2?extends?book{ Scanner?sc?=?new?Scanner(System.in); public?void?show1(){?? super.sys2(); try{ int?number?=?sc.nextInt(); if(number?==1){ super.gs(); }else?if?(number?==?2){ super.yy(); } }catch(Exception?e){ super.bcz(); }? } //用show2方法來儲(chǔ)存用圖書名搜索的圖書程序 public?void?show2(){ super.sys1(); try?{ String?title?=?sc.nextLine(); if?(title.equals("高數(shù)")){ super.gs(); }else?if?(title.equalsIgnoreCase("論語")){ super.ly(); }else{ super.bcz(); } }catch(Exception?e){ super.sys3(); } } }
package?tryCatchfinally; //用來儲(chǔ)存所用到的方法 public??class?book?{ ?? public?void?books(){ System.out.println("輸入命令:1-按照名稱查詢圖書;2-按照序號(hào)查詢圖書"); } public?void?sys1(){ System.out.println("請(qǐng)輸入圖書名字"); } public?void?sys2(){ System.out.println("請(qǐng)輸入圖書編號(hào)"); } public?void?sys3(){ System.out.println("請(qǐng)按提示輸入,請(qǐng)重新輸入"); } public?void?ly(){ System.out.println("論語"); } public?void?yy(){ System.out.println("音樂"); } public?void?gs(){ System.out.println("高數(shù)"); } public?void?bcz(){ System.out.println("圖書不存在"); } }
問題都在注釋里寫了?
目前最終的代碼
目前在if外面套了一個(gè)while循環(huán)就可以繼續(xù)執(zhí)行了 但是不清楚原理 有大神能給說說嗎
舉報(bào)
Java中你必須懂得常用技能,不容錯(cuò)過的精彩,快來加入吧
4 回答關(guān)于finally執(zhí)行問題
2 回答try-catch-finally執(zhí)行順序
2 回答為什么finally沒有執(zhí)行?
4 回答如果catch沒有執(zhí)行會(huì)執(zhí)行finally嗎??
2 回答不是return過后就退出了嗎,finally可以繼續(xù)執(zhí)行,那為什么finally之后的也能執(zhí)行呢
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-08-05
沒見過在finally里這么干的,finally一般是用來釋放資源的,不會(huì)在里面做一些等待用戶輸入的操作。
2016-08-05
問題都在注釋里寫了?
目前最終的代碼
2016-08-04