簡體   English   中英

檢查輸入是否為while循環中的數字

[英]Check if input is a number in a while loop

我開始用 Java 編寫代碼(以前從未這樣做過),而且我對輸入驗證感到頭疼。

我需要當用戶輸入從 0 到 1000 的數字時,while 循環會不斷執行。 這很好用。 問題是我想檢查他是否輸入了一個數字,如果他沒有輸入,while 循環應該繼續執行並等待下一個輸入。 到目前為止,我的代碼只在輸入不是數字的東西時拋出 InputMismatchException ,我不明白為什么。

這是我的代碼:

Scanner score = new Scanner(System.in);
    int i = 0;
    while (i < 1000000) {
        System.out.print("Insert the new score: ");
        if (score.hasNextInt()) {
            i = score.nextInt();
            if (i > 0) {
                if (i < 200000) {
                    // do something
                } else if (i < 500000) {
                    // do something
                } else if (i < 700000) {
                    // do something
                } else if (i < 100001) {
                    // do something
                }
            } else if (i < 0) {
                // do something else
            }
        } else {
            System.out.print("The score should be a number.");
            i = score.nextInt();
        }
    }
else{
    System.out.print("The score should be a number.");
    i = score.nextInt(); //<---problem
}

在這里,您知道 input 不是數字,因此您不應該嘗試使用nextInt()將其讀取為int 所以要消耗無效數據使用

您收到此錯誤是因為您告訴Scanner獲取整數,即使用戶未鍵入整數。 您需要接受所有輸入(通過String )並自己進行驗證。

我已經修改了您的代碼以將輸入作為字符串( Scanner#nextLine ),然后嘗試更新分數。

Scanner score = new Scanner(System.in);
int i = 0;
while (i < 1000000) {
    System.out.print("Insert the new score: ");
    if (score.hasNext()) {
        final String input = score.nextLine();
        try {
            i = Integer.parseInt(input);
        } catch (final Exception e) {
            System.out.print("The score should be a number.");
            continue;
        }
    }
    if (i>0) {
        if (i<200000) {
            //do something
        }
        else if (i<500000) {
            //do something
        }
        else if (i<700000) {
            //do something
        }
        else if (i<100001) {
            //do something
        }
    } else if (i<0) {
        //do something else
    }
}

注意continue; 語句將無限期地要求用戶輸入整數,直到用戶輸入為止,正如您在問題中所要求的那樣。

在最后的 else 子句中,您調用了score.nextInt()盡管您知道下一個數字不是 int。 這就是為什么你會得到例外。 只需用這個替換 else :

else {
    System.out.print("The score should be a number.");
    score.nextLine(); //This is the line you need to change
}

score.nextLine(); 將安全地消耗下一行輸入,並使您回到循環中。

您需要將輸入語句包裝在 try/catch 塊中並捕獲 InputMisMatchException。 InputMisMatchException 的發生是因為在需要整數的 Scanner 中使用 nextInt() 。

Scanner input = new Scanner(System.in);
int i = 0;
do {
    try {
        System.out.print("Enter integer ");
        i = input.nextInt();
    } catch (InputMismatchException e) {
       //anything but integer gets caught here.
        System.out.print("Please enter only integer.");
    }
    //necessary to take the cursor back to start of line, clear the buffer
     input.nextLine();

} while (i < 1000000);

上面的代碼示例顯示了如何捕獲 InputMisMatchException。

我注意到您在代碼中使用了input.hasNextInt() ,您需要的只是替換代碼的 else 塊中的以下內容。

i = score.nextInt()

score.nextLine(); 

如其他答案中所述,這句話將清除等待下一個輸入的緩沖區。 異常是由於 score.nextInt(),替換應該修復它。

您可能想用 try-catch 塊包裝 while 內容。

Scanner score = new Scanner(System.in);
int i = 0;
while (i < 1000000){
try{
    System.out.print("Insert the new score: ");
    if (score.hasNextInt()){
    i = score.nextInt();
    if (i>0){
        if (i<200000){
            do something
        }
        else if (i<500000){
            do something
        }
        else if (i<700000){
            do something
        }
        else if (i<100001){
            do something
        }
    }else if (i<0){
        do something else
    }
    }else{
        System.out.print("The score should be a number.");
        i = score.nextInt();
    }
}catch(Exception e){}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM