0

我是编程新手,我一直在编写一个简单的程序来创建一个继续显示错误的石头剪刀布游戏。在启动/运行该程序时,它似乎无法识别变量 Rock Paper 和 Scissors 并直接进入 else 条件。另外,当我尝试解析之后

int exit = 4;
while (exit != userchoice){
JOptionPane.showInputDialog("Choose wisely. Enter:\n 1 for Rock \n 2 for Paper "
        + "\n 3 for Scissors \n or 4 to Exit:\n");

该程序就停在那里,所以我仍然很难找到任何其他方式。另外,这个程序将如何以我要去的条件结束?我仍然不明白如何结束这个循环,因为我认为用一个简单的最终括号它会在满足条件后结束循环,在这种情况下输入数字 4。

帮助将不胜感激。谢谢你。

    public static void main(String[] args) {
    String inString = null;
    int compchoice = (int)(Math.random() * 3);
    int userchoice = 0;
    int Rock = 1, Paper = 2, Scissors = 3;

    Scanner input = new Scanner(System.in);
    JOptionPane.showMessageDialog(null, 
            "Welcome to JanKenPo! It is You Vs. The Computer\n");

    int exit = 4;
    while (exit != userchoice){
    JOptionPane.showInputDialog("Choose wisely. Enter:\n 1 for Rock \n 2 for Paper "
            + "\n 3 for Scissors \n or 4 to Exit:\n");

    if (userchoice == Rock && compchoice == Paper) {
            JOptionPane.showMessageDialog(null, "Paper covers Rock! You win this Round.");
            }             
    else if (userchoice == Rock && compchoice == Scissors){
            JOptionPane.showMessageDialog(null, "Rock crushes Scissors! You win this Round.");
            }        
    else if (userchoice == Scissors && compchoice == Paper){
            JOptionPane.showMessageDialog(null, "Scissors cuts Paper! You win this round.");
            }
    else if (userchoice == Paper && compchoice == Rock){
            JOptionPane.showMessageDialog(null, "Paper Covers Rock! Computer wins this Round.");
            }
    else if (userchoice == Paper && compchoice == Scissors){
            JOptionPane.showMessageDialog(null, "Scissors cuts Paper! Computer wins this Round.");
            }
    else if (userchoice == Scissors && compchoice == Rock){
            JOptionPane.showMessageDialog(null, "Rock crushes Scissors! Computer wins this Round.");
            }
    else if (userchoice == compchoice){
            JOptionPane.showMessageDialog(null, "It's a Tie! Both you and the computer chose the same object.");
        }
    else{
        exit = Integer.parseInt(inString);
        }
    }   
}
JOptionPane.showMessageDialog(null, "Good Bye!");
}
4

2 回答 2

1

你需要做

String input = JOptionPane.showInputDialog(...);
userChoice = Integer.parseInt(input);

现在,您只是完全忽略输入而不是设置userChoice:)

于 2013-03-06T19:16:32.413 回答
0

好吧,您还需要执行上述操作:您不需要扫描器,因为您从 showInputDialog() 获取输入;添加你的while循环体{

String input = JOptionPane.showInputDialog(...);

userChoice = Integer.parseInt(input);

int compchoice = (int)(Math.random() * 3) +1;

通过添加 +1,您将获得 b/w 1-3 输出,并且您需要将其添加到 while 循环中,因为计算机需要在每次循环运行时做出选择。

还要从 else 块中删除该行 exit =Integer.parseInt(inString);

当用户输入选项 4 时,您的程序将终止;

于 2013-03-06T20:57:47.587 回答