Re: [問題] 讀入字串判斷是否為數字的問題
我其實是這樣寫的
這是某桃園一帶某私立大學大一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
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
03/06 11:16, 7F
推
03/06 11:33, , 8F
03/06 11:33, 8F
→
03/06 11:43, , 9F
03/06 11:43, 9F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 6 篇):