0
我试图让它在玩完游戏并确定胜出者后,会询问用户是否想再次玩。我添加了“do while”循环,但似乎没有工作。输出始终是沿着线的东西:确认对话框不起作用
- 玩游戏
- 获得冠军/输/领带和得分
- 问你要选择再次
- 什么,如果用户想接着问发挥再次
- 即使用户说不,它不会停止游戏
我不知道是什么问题,因为我很新的编码。谢谢!
/*
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(playAgain == 0){ do {0} {0} {0} String playerChoice =(String)JOptionPane.showInputDialog(null,“1,2,3,Shoot:”, “Rock,Paper,Scissor”,JOptionPane.QUESTION_MESSAGE, null,playerOptions,playerOptions [0]); //获得玩家选择 int computerChoice =(int)(Math.random()*(3)); //获得计算机随机选择 while(playerChoice.equals(“Rock “)|| playerChoice.equals(”Paper“)|| playerChoice.equals(”Scissors“)) {' – nbzimm365
也许用新代码编辑您的原始问题。我看不懂。 – Michael