2017-04-21 4 views
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); 
    } 
} 

답변

0

귀하의 루프는 당신은 예를 들어, 거의 주위에 게임의 모든 로직을 할 - 동안 루프를 이동하려는

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

주위 잘못된 방법입니다

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

그래서 시도해 보았습니다. 그렇지만 나는 여전히 올바르게했는지 확신 할 수 없습니다. 대괄호로 코드를 닫으려고하면 오류가 계속 발생합니다. (0, 0) { { String playerChoice = (String) JOptionPane.showInputDialog (null, "1, 2, 3, Shoot :", "Rock, Paper, Scissor", JOptionPane.QUESTION_MESSAGE, // 컴퓨터 선택을 무작위로 얻습니다. while (playerChoice.equals ("Rock()", "PlayerOptions, playerOptions [0] ") || playerChoice.equals ("Paper ") || playerChoice.equals ("Scissors ")) {' – nbzimm365

+0

새 코드로 원래 질문을 편집 할 수 있습니다. 나는 그것을 읽을 수 없다. – Michael

관련 문제