所以,我一直在尋找一種有效的方法,使用Java的標準包來讀取輸入整數...例如,我碰到類「掃描儀」,但我發現了兩個主要困難:Java:如何讀取輸入int
- 如果我不插入int,我實際上不能解決異常;
- 該類適用於令牌,但我的目標是加載字符串的全長。
這就是我想實現執行的例子:
Integer: eight
Input error - Invalid value for an int.
Reinsert: 8 secondtoken
Input error - Invalid value for an int.
Reinsert: 8
8 + 7 = 15
這是(不正確)的代碼,我試圖執行:
import java.util.Scanner;
import java.util.InputMismatchException;
class ReadInt{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean check;
int i = 0;
System.out.print("Integer: ");
do{
check = true;
try{
i = in.nextInt();
} catch (InputMismatchException e){
System.err.println("Input error - Invalid value for an int.");
System.out.print("Reinsert: ");
check = false;
}
} while (!check);
System.out.print(i + " + 7 = " + (i+7));
}
}
請你詳細說明你是什麼意思,如果我不插入int,我實際上不能解決異常'? –