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

看板java作者 (我是道明柏毅)時間18年前 (2008/03/05 18:19), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串4/6 (看更多)

03/05 00:57,
這 很正常啊 因為你這個變數是在while內宣告
03/05 00:57

03/05 00:59,
我會用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("執行成功"); } -- ******************************************************** * Appleboy Blog * * http://blog.Wu-Boy.com/ * * Appleboy Album * * http://pic.wu-boy.com * ******************************************************** -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.107.41

03/05 19:04, , 1F
真要用,還是把exception寫細,直接寫個大類上去不合适...
03/05 19:04, 1F
※ 編輯: appleboy46 來自: 140.123.107.41 (03/05 21:02)

03/05 21:02, , 2F
樓上您說的是我改過的這樣嗎?
03/05 21:02, 2F

03/05 22:13, , 3F
那個parseInt是會拋NumberFormatException,而不是那啥數學
03/05 22:13, 3F
文章代碼(AID): #17pdEOcK (java)
討論串 (同標題文章)
文章代碼(AID): #17pdEOcK (java)