Re: [問題] 讀入字串判斷是否為數字的問題
※ 引述《divaka (豬肉腳)》之銘言:
: 大家好,小弟目前已經可以成功讀入字串並且判斷是否為數字,部份的 code 如下
try catch有其意義,處理Exception除了直接catch以外,
還有另外一個方式叫做預先檢測。(也就是你原本所用的方式)
try
{
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("請輸入數字:");
String inputStr = input.readLine();
while (inputStr == null || !inputStr.matches("[0-9]+"))
{
System.out.println("輸入錯誤,請重新輸入數字:");
inputStr = input.readLine();
}
int num=Integer.parseInt(inputStr);
System.out.println("輸入的數字是:"+num);
}
catch (IOException e) //for readLine()
{
e.printStackTrace();
}
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.115.77.239
→
03/06 10:35, , 1F
03/06 10:35, 1F
→
03/06 11:10, , 2F
03/06 11:10, 2F
→
03/06 11:18, , 3F
03/06 11:18, 3F
→
03/06 11:20, , 4F
03/06 11:20, 4F
推
03/06 11:23, , 5F
03/06 11:23, 5F
→
03/06 11:25, , 6F
03/06 11:25, 6F
推
03/06 11:26, , 7F
03/06 11:26, 7F
推
03/21 01:10, , 8F
03/21 01:10, 8F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 6 之 6 篇):