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