我试图让它在玩游戏并确定胜利者之后,它将询问用户是否想再玩一次。我添加了一个“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);
}
}
答案 0 :(得分: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);