Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确认对话框不工作_Java_Swing_Conditional - Fatal编程技术网

Java 确认对话框不工作

Java 确认对话框不工作,java,swing,conditional,Java,Swing,Conditional,我正在尝试这样做,在你玩游戏并确定胜利者后,它会询问用户是否想再次玩。我添加了一个“DoWhile”循环,但它似乎不起作用。输出始终是以下内容: 玩游戏 获得赢家/输家/平局并得分 再次询问您想要选择什么 然后询问用户是否想再次播放 即使用户说不,也不会停止游戏 我不确定问题是什么,因为我对编码还很陌生。谢谢 /* Playing Rock Paper Scissors against a computer would be really boring if we always knew

我正在尝试这样做,在你玩游戏并确定胜利者后,它会询问用户是否想再次玩。我添加了一个“DoWhile”循环,但它似乎不起作用。输出始终是以下内容:

  • 玩游戏
  • 获得赢家/输家/平局并得分
  • 再次询问您想要选择什么
  • 然后询问用户是否想再次播放
  • 即使用户说不,也不会停止游戏
我不确定问题是什么,因为我对编码还很陌生。谢谢

/*
Playing Rock Paper Scissors against a computer would be really boring if we 
always knew what the computer was going to choose, or we created a program 
that has a distinct pattern. In order to increase replayability of your game
you will want to randomize the computer's choice.

For our game we will have the computer generate a random number (0, 1, or 2) 
which will correspond to one of the choices (i.e. 0 = Rock, 1 = Paper, 2 = 
Scissors)  
 */

import javax.swing.*;
public class rockPaperScissors {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int playAgain = 0;
        int score = 0;
        String [] playerOptions = {"Rock","Paper","Scissor"};
        String playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);//Gets players pick
        int computerChoice = (int)(Math.random()*(3));//gets computers choice randomly
        while (playerChoice.equals("Rock") || playerChoice.equals("Paper") || playerChoice.equals("Scissors")) {

            do {
                if (computerChoice == 0 && playerChoice.equals("Rock")) {
                    JOptionPane.showMessageDialog(null,"Tied! You both chose rock.");//determines winner and prints only for tied and wins for user
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else if (computerChoice == 0 && playerChoice.equals("Paper")) {
                    JOptionPane.showMessageDialog(null,"You won!");

                    score = score+1;
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else if (computerChoice == 1 && playerChoice.equals("Paper")){
                    JOptionPane.showMessageDialog(null,"Tied! You both chose Paper!");
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else if (computerChoice == 1 && playerChoice.equals("Scissor")){
                    JOptionPane.showMessageDialog(null,"You won!");
                    score = score+1;
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else if (computerChoice == 2 && playerChoice.equals("Scissor")) {
                    JOptionPane.showMessageDialog(null,"Tied! You both chose scissor!");
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else if (computerChoice == 2 && playerChoice.equals("Rock")){
                    JOptionPane.showMessageDialog(null,"You won!");
                    score = score+1;
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                } else { //if user lost 
                    JOptionPane.showMessageDialog(null,"You lost! Try again!");
                    score = score-1;
                    System.out.println(score);

                    playerChoice = (String)JOptionPane.showInputDialog(null,"1, 2, 3, Shoot:",
                            "Rock, Paper, Scissor",JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);
                }
                playAgain = JOptionPane.showConfirmDialog(null, "Play again?");
            } while (playAgain == 0);
        }
        System.out.println("Game Ended");
        System.exit(0);
    }
}

你的循环是错误的

while (playerChoice.equals("Rock") || playerChoice.equals("Paper") || /*...*/) 
{
    do {
        // did they win?
        // ...
        playAgain = JOptionPane.showConfirmDialog(null, "Play again?");
    } while (playAgain == 0);
}
你想把do-while循环移动到几乎所有游戏的逻辑上,例如

do {
    // get input and computer choice
    while (playerChoice.equals("Rock") || playerChoice.equals("Paper") || /*...*/)
    {
        // did they win?
        // ...
    }
    playAgain = JOptionPane.showConfirmDialog(null, "Play again?");
} while (playAgain == 0);

所以我试过了,但我不确定我是否还是做对了。当我试图用括号关闭代码时,它总是给我一个错误<代码>while(playreach==0){do{String playerChoice=(String)JOptionPane.showInputDialog(null,“1,2,3,shot:”,“摇滚,纸,剪刀”,JOptionPane.QUESTION_MESSAGE,null,playerOptions,playerOptions[0]);//让玩家选择int计算机选项=(int)(Math.random()*(3));//在(playerChoice.equals(“石头”)| playerChoice.equals(“纸”)| playerChoice.equals(“剪刀”){也许可以用新代码编辑你原来的问题。我看不懂。