Java:石头、剪刀、布即将完成,但出现 fatal error

标签 java if-statement nested switch-statement

我的任务是创建一个类似于石头、剪刀、布的 Java 程序。我在下面写了我认为可行的内容。然而,只有当用户选择 R 或 r(代表摇滚)时,它才能真正正常工作。如果用户选择 s 或 p(剪刀或布),代码将完全崩溃或给出多个答案。我查看了代码,似乎无法找出它无法正常工作的原因。

此外,任何有关如何更好地编写代码的 switch 部分的建议将不胜感激。我感觉我写的方式不正确。

import java.util.Scanner;

public class Project_2 {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int compVal = (int) (3 * Math.random()) + 1;
        String compActual = "";
        System.out.println("Welcome to Rock, Paper, Scissors!");
        System.out.print("Enter r for rock, p for paper, or s for scissors: ");
        String userOriginal = keyboard.nextLine();
        userOriginal = (userOriginal.toUpperCase());
        switch (userOriginal) {
        case "r":
            userOriginal = userOriginal;
            break;
        case "R":
            userOriginal = userOriginal;
            break;
        case "p":
            userOriginal = userOriginal;
            break;
        case "P":
            userOriginal = userOriginal;
            break;
        case "s":
            userOriginal = userOriginal;
            break;
        case "S":
            userOriginal = userOriginal;
            break;
        default:
            System.out.println("Invalid input, please try again!");
            System.exit(1); // This will exit the program if invalid input is
                            // given. The 1 signifies that it ended with an
                            // error.
        }
        if (compVal == 1) {
            compActual = "R";
        } else if (compVal == 2) {
            compActual = "P";
        } else if (compVal == 3) {
            compActual = "S";
        }
        if (compActual.equals(userOriginal)) {
            System.out.println("It was a tie!");
        } else if (compActual.equals("R"))
            if (userOriginal.equals("S")) {
                System.out.println("You played Scissors and I chose Rock: Rock crushes Scissors so I win this time!");
            }
        if (userOriginal.equals("P")) {
            System.out.println("You played Paper and I chose Rock: Paper covers Rock so you win this time!");
        } else if (compActual.equals("S"))
            if (userOriginal.equals("R")) {
                System.out.println("You played Rock and I chose Sciccors: Rock crushes Scissors so you win this time");
            }
        if (userOriginal.equals("P")) {
            System.out.println("You played Paper and I chose Scissors: Paper is cut by Scissors so I win this time!");
        } else if (compActual.equals("P"))
            if (userOriginal.equals("R")) {
                System.out.println("Your played Rock and I chose Paper: Paper covers rock so I win this time!");
            }
        if (userOriginal.equals("S")) {
            System.out.println("You played Scissors and I chose Paper: Scissors cuts Paper so you win this time!");
        }

    }
}

最佳答案

按照建议,您应该稍微清理一下代码。为什么不使用数值而不是字符串进行比较,如下所示:

        int compVal = (int) (3 * Math.random()) + 1;
        int userOriginal = 0;
        String userInput = (keyboard.nextLine().toUpperCase());

        switch (userInput) {
        case "R":
            userOriginal = 1;
            break;
        case "P":
            userOriginal = 2;
            break;
        case "S":
            userOriginal = 3;
            break;
        default:
            System.out.println("Invalid input, please try again!");
            System.exit(1); // This will exit the program
        }

之后,您可以将用户提供的值与生成的值进行比较:

        if (compVal == userOriginal) {
            System.out.println("It was a tie!");
        } else
        {
            //R
            if (compVal == 1)
            {
                if (userOriginal == 2) {
                    System.out.println("You played Paper and I chose Rock: Paper covers Rock so you win this time!");
                }
                if (userOriginal == 3) {
                    System.out.println("You played Scissors and I chose Rock: Rock crushes Scissors so I win this time!");
                }
            }
            //P 
            if (compVal == 2)
            {
                if (userOriginal == 1) {
                    System.out.println("Your played Rock and I chose Paper: Paper covers rock so I win this time!");
                }
                if (userOriginal == 3) {
                    System.out.println("You played Scissors and I chose Paper: Scissors cuts Paper so you win this time!");
                }
            }
            //S
            if (compVal == 3)
            {
                if (userOriginal == 1) {
                    System.out.println("You played Rock and I chose Sciccors: Rock crushes Scissors so you win this time");
                }
                if (userOriginal == 2) {
                    System.out.println("You played Paper and I chose Scissors: Paper is cut by Scissors so I win this time!");
                }
            }
        }

此外,请记住始终关闭您使用的所有资源:

keyboard.close();

关于Java:石头、剪刀、布即将完成,但出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32670304/

相关文章:

javascript - jQuery if 语句的问题

java - 使用minimal-json 创建嵌套JSON?

javascript - 为什么 "window"对象有这么多嵌套的 "window"对象?

java - 通过外部库在 android 中使用更高的 API 级别

java - 上下文组件扫描找不到我的自定义注释 bean

java - Log4j 2 JSON 配置

Java:无法正确搜索数组

java - 股票价格的 BigDecimal?

C代码: if do/while won't do what I ask it to do

c++ - 使用嵌套的 if 语句来构造代码