2017-04-21 2 views
0

Ich versuche, es so zu machen, dass, nachdem Sie das Spiel spielen und den Gewinner bestimmen, es fragt, ob der Benutzer wieder spielen möchte. Ich habe eine "do while" -Schleife hinzugefügt, aber es scheint nicht zu funktionieren. Der Ausgang ist immer etwas entlang der Linien von:Confirm Dialog funktioniert nicht

  • Spielen Sie das Spiel
  • Holen Sie sich den Sieger/Verlierer/tie und sammeln
  • Fragen Sie, was Sie wieder
  • dann fragen wählen möchten, wenn der Benutzer wünscht, wieder zu spielen
  • auch wenn der Benutzer sagt, nein, stoppt es das Spiel nicht

ich bin nicht sicher, was das Problem ist, wie ich ziemlich neu bin Codierung. Vielen Dank!

/* 
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); 
    } 
} 

Antwort

0

Ihre Schleifen sind der falsche Weg, um

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

Sie wollen die do-while-Schleife um so ziemlich alle das Spiel der Logik bewegen z.B.

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); 
+0

Also habe ich das versucht, aber ich bin mir nicht sicher, ob ich es noch richtig gemacht habe. Es gibt mir einen Fehler, wenn ich versuche, den Code mit einer Klammer zu schließen. 'while (playAgain == 0) { do { Zeichenfolge playerChoice = (Zeichenfolge) JOptionPane.showInputDialog (null," 1, 2, 3, Shoot: ", " Rock, Papier, Scissor ", JOptionPane.QUESTION_MESSAGE, null, playerOptions, playerOptions [0]); // Ruft die Spieler ab int computerChoice = (int) (Math.random() * (3)); // wahlt die Computer wahlfrei while (playerChoice.equals ("Rock ") || playerChoice.equals (" Paper ") || playerChoice.equals (" Scissors ")) {' ​​ – nbzimm365

+0

Vielleicht bearbeiten Sie Ihre ursprüngliche Frage mit dem neuen Code. Ich kann das nicht lesen. – Michael

Verwandte Themen