Re: [問題] 讀入字串判斷是否為數字的問題

看板java作者 (不再嘴砲了)時間18年前 (2008/03/06 09:54), 編輯推噓5(504)
留言9則, 3人參與, 最新討論串5/6 (看更多)
我其實是這樣寫的 這是某桃園一帶某私立大學大一java程式設計作業~_~(泣~我的水準只能接這種外包) BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("請輸入你要的數字:"); String cin =new String(); while(true){ cin = buf.readLine(); try{ int test = Integer.parseInt(cin); break; } catch(NumberFormatException e) { System.out.println("這不是數字"); continue; //寫辛酸的 } } ※ 引述《appleboy46 (我是道明柏毅)》之銘言: : 推 slalala:這 很正常啊 因為你這個變數是在while內宣告 03/05 00:57 : 推 slalala:我會用Integer.parseInt(String str) 用Try catch去寫這題 03/05 00:59 : 您推的是這種嗎? : BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); : try{ : System.out.print("請輸入你要的數字:"); : int test = Integer.parseInt(buf.readLine()); : } : catch(ArrayIndexOutOfBoundsException e) : { : System.out.println(e.toString() + "陣列程式發生錯誤"); : } : catch(ArithmeticException e) : { : System.out.println(e.toString() + "數學發生錯誤"); : } : catch(Exception e) : { : System.out.println(e.toString() + "程式發生錯誤"); : } : finally : { : System.out.println("執行成功"); : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.59.147.201

03/06 09:57, , 1F
應該不會是我們系上得學弟妹外包的吧 XD
03/06 09:57, 1F

03/06 10:33, , 2F
被發現了
03/06 10:33, 2F

03/06 10:40, , 3F
你們學校寒假作業有夠多的
03/06 10:40, 3F

03/06 10:55, , 4F
那群(...) 我早就把答案放在他們拿的到的地方了....
03/06 10:55, 4F

03/06 10:57, , 5F
那些寒假作業是從我大一之前就有的東西..幾年來都沒要求
03/06 10:57, 5F

03/06 11:00, , 6F
算了 -_-;; 系上風氣不是我該/能去擔心跟關心的
03/06 11:00, 6F

03/06 11:16, , 7F
這跟學校沒關係 程式設計的課大多這樣 不懂語法可以幹SA勒
03/06 11:16, 7F

03/06 11:33, , 8F
淦... 那我資傳的畢製能不能外包啊 ......OTL
03/06 11:33, 8F

03/06 11:43, , 9F
我打算創業 進駐大學院區 公司名稱"專題外包"
03/06 11:43, 9F
文章代碼(AID): #17pqxoff (java)
討論串 (同標題文章)
文章代碼(AID): #17pqxoff (java)