簡體   English   中英

如果將字符串放在int輸入變量上,如何添加else語句?

[英]How to add else statement if they put a string on an int input variable?

每當我運行此代碼時,如果我輸入字符串作為輸入,我在第11行都會收到錯誤消息。 我不確定如何運行else語句,或者詢問是否放置字符串而不是整數(選擇變量)的內容。 我是新手,非常感謝您的幫助!

這是我的代碼:

import java.util.Scanner;

public class rockPaper {
    public static void main(String args[]) {
        System.out.println("Hello world");
        int rock = 0;
        int paper = 1;
        int scissors = 2;
        Scanner input = new Scanner(System.in);
        System.out.println("Rock, Paper, or Scissors(0, 1, or 2)? Enter your integer: ");
        int choice = input.nextInt();
        int aIChoice = (int) (Math.random() * 3);
        if (choice == rock) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("Rock ties with rock!");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("Fail. Paper trumps rock.");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("You win! Rock trumps scissors.");
                break;
            default:
                break;
            }

        } else if (choice == paper) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("You win! Paper trumps rock.");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("Paper ties with paper!");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("Fail. Scissors trumps paper.");
                break;
            default:
                break;
            }
        } else if (choice == scissors) {
            switch (aIChoice) {
            case 0:
                System.out.println("AI chose rock.");
                System.out.println("Fail. Rock trumps scissors.");
                break;
            case 1:
                System.out.println("AI chose paper.");
                System.out.println("You win! Scissors trumps paper.");
                break;
            case 2:
                System.out.println("AI chose scissors.");
                System.out.println("Scissors ties with scissors!");
                break;
            default:
                break;
            }
        } else {
            System.out.println("Nope!");
        }
        input.close();
    }

}

如上所述,如果我運行代碼,並鍵入任何字母(字符串),則會出現錯誤,指出行號為11。 我不確定應該怎么做,因為很顯然,正如我已經提到的,在所有這些語句中添加一個else語句並不能確保它們輸入字符串時不會出現“ nope”。

通常您不會接受字符串輸入。 如果確實出於某些原因需要,可以將輸入行替換為String choice = input.next()然后對其進行任何測試。 確認為輸入后,您要參考此文章以將String轉換為int。

如果用戶提供了無效的輸入,您不太確定會怎樣,但是,我認為,如果輸入不是有效的整數,則合理的選擇是檢查輸入並向用戶顯示錯誤消息。 為此,只需在下面而不是代碼中添加以下代碼即可: int choice = input.nextInt();

int choice = -1;
if (input.hasNextInt()) {
  choice = input.nextInt();
} else {
  System.out.println("Please provide valid integer input: Rock, Paper, or Scissors(0, 1, or 2)?");
}

PS另外,最好檢查您的整數是否在[0,2]范圍內。

暫無
暫無

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

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