【问题标题】:Inputted string also assigned to an integer?输入的字符串也分配给整数?
【发布时间】:2023-03-12 16:13:01
【问题描述】:

我正在尝试制作一个三分之二的石头剪刀布程序,其中计算机随机滚动 0-2 并且每个都分配给石头、纸或剪刀,然后它比较 userInput 并计算计算机或玩家的胜利,然后将其相加。

但是,我不知道如何让用户输入“剪刀”,程序会知道它也被分配给 2(用于比较目的)。

    public static void main(String[] args) {
    Random r = new Random();
    int gameCount = 0;
    int computerWins = 0;
    int playerWins = 0;
    int rock = 0;
    int paper = 1;
    int scissors = 2;
    int playerChoice;
    int computerChoice = r.nextInt(3);

    System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");

    while (gameCount >= 0 && gameCount < 3)
    {
        System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\"");
        break;
    }
       playerChoice = userInput.nextInt()

        //If player enters anything besides rock, paper, or scissors
        if (playerChoice < 0 || playerChoice >= 3) {
            System.out.println("That wasn't an option");
            computerWins++;
            gameCount++;

          //The game goes on, and the winners are added up!
        } else if (playerChoice == 0 && computerChoice == 1) {
            computerWins++;
            gameCount++;
            System.out.println("Rock v Paper! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 1 && computerChoice == 0) {
            playerWins++;
            gameCount++;
            System.out.println("Paper v Rock! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 1 && computerChoice == 2) {
            computerWins++;
            gameCount++;
            System.out.println("Paper v Scissors! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 1) {
            playerWins++;
            gameCount++;
            System.out.println("Scissors v Paper! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 0) {
            computerWins++;
            gameCount++;
            System.out.println("Scissors v Rock! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 0 && computerChoice == 2) {
            playerWins++;
            gameCount++;
            System.out.println("Rock v Scissors! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 0 && computerChoice == 0) {
            gameCount++;
            System.out.println("Rock v Rock! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 1 && computerChoice == 1) {
            gameCount++;
            System.out.println("Paper v Paper! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 2) {
            gameCount++;
            System.out.println("Paper v Paper! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        }

        //Check if game count reaches max games then chooses a winner
        if (gameCount == 3 && computerWins > playerWins) {
            System.out.println("The Computer Wins!");
        } else if (gameCount == 3 && computerWins < playerWins) {
            System.out.println("The Player Wins!");
        } else if (gameCount == 3 && computerWins == playerWins) {
            System.out.println("The game is a tie!");
        }
    }
}

【问题讨论】:

  • 看看枚举,它们会解决你的问题

标签: java


【解决方案1】:

所以不要尝试playerChoice = userInput.nextInt();,试试这个:

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
try {
    playerChoice = Integer.parseInt(input);
} catch (NumberFormatException e) {
    if (input.equalsIgnoreCase("rock")) {
        playerChoice = rock;
    } else if (input.equalsIgnoreCase("paper")) {
        playerChoice = paper;
    } else if (input.equalsIgnoreCase("scissors")) {
        playerChoice = scissors;
    } else {
        // if input is invalid
        playerChoice = -1;
    }
}

由于您使用的是 userInput.nextInt()playerChoice 是一个 int 并且只能保存 int,因此您需要解析用户的输入。在这种情况下,Integer.parseInt(input) 将尝试在用户输入中查找 int。如果不能,它将返回异常;这就是为什么有一个 try-catch 块。如果它不是 int,它将查找每个字符串并将关联的 int 值分配给playerChoice,如果输入无效,则为 -1。然后,您的其余代码应该能够在此之后适当地处理 playerChoice。

【讨论】:

  • 谢谢!我会做到的!
猜你喜欢
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多