2016-09-21 4 views
1

私はロックペーパーのはさみゲームを作っています。コンピューターやユーザーがベスト2/3を選んだら、ゲームをもう一度やりたいのであればプロンプトが表示されます。 "はい"の場合はプログラムが再起動し、 "いいえ"の場合はsystem.exitと推測されます。Java:再度プレイするかどうかをユーザーに確認しますか?

どうすればよいですか?

public static void main(String[] args) { 
    Random r = new Random(); 
    int gameCount = 0; 
    int computerWins = 0; 
    int playerWins = 0; 
    int rock, paper, scissors; 
    rock = 1; 
    paper = 2; 
    scissors = 3; 
    int playerChoice = 0; 
    int computerChoice; 

    System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!"); 

    //While the game count is less than 3, the loop will repeat 
    while (gameCount < 3) { 
     computerChoice = r.nextInt(3) + 1; 
     System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\""); 
     String USER_Input = userInput.next(); 
     if (USER_Input.equalsIgnoreCase("Rock")) { 
      playerChoice = 1; 
     } 
     if (USER_Input.equalsIgnoreCase("Paper")) { 
      playerChoice = 2; 
     } 
     if (USER_Input.equalsIgnoreCase("Scissors")) { 
      playerChoice = 3; 
     } 
     //If player enters anything besides rock, paper, or scissors 
     if (playerChoice <= 0 || playerChoice > 3) { 
      System.out.println("That wasn't an option"); 
      computerWins++; 
      gameCount++; 
      System.out.println("Not a valid input! Computer Wins!\n" + 
        "Player has won " +playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 

      //The game goes on, and the winners are added up! 
     } else if (playerChoice == 1 && computerChoice == 2) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Rock v Paper! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 1) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Paper v Rock! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 3) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Paper v Scissors! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 2) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Scissors v Paper! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 1) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Scissors v Rock! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 1 && computerChoice == 3) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Rock v Scissors! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 1 && computerChoice == 1) { 
      gameCount++; 
      System.out.println("Rock v Rock! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 2) { 
      gameCount++; 
      System.out.println("Paper v Paper! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 3) { 
      gameCount++; 
      System.out.println("Paper v Paper! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } 

     //Check if game count reaches max games then chooses a winner 
     if (gameCount == 3 && computerWins > playerWins || computerWins == 2) { 
      System.out.println("The Computer Wins!"); 
      break; 
     } else if (gameCount == 3 && computerWins < playerWins || playerWins ==2) { 
      System.out.println("The Player Wins!"); 
     } else if (gameCount == 3 && computerWins == playerWins) { 
      System.out.println("The game is a tie!"); 
     } 
    } 
} 
+0

ユーザ入力をチェックし、それが "yes"か何かの場合は、メソッドmain()を呼び出します。 – ivan

+0

''これはオプションではありませんでした」=> computerWins ++ 'あなたは+1を取得します –

+0

ハハありがとうfr_andres;)コンピュータは常に勝つ! @ivanメインメソッドをもう一度呼び出すことができませんでした。それは面白いです。 –

答えて

3

ロジック全体がmain() - メソッド内にあります。 while()部分をplay()のような新しいメソッドに移動することをお勧めします。

メインで

()あなたのメッセージを促すと、キーボードからの入力を読んで新しいしばらく()を実装します。

public static void main(String args[]) { 
     Scanner inputScanner = new Scanner(System.in); 
     String userInput = ""; 
     do { 
      play(); 
      System.out.print("Do you want to play again ([Y]/n)? "); 
      userInput = inputScanner.nextLine(); 
      System.out.println(userInput); 
     } while (userInput.equals("Y") || userInput.equals("")); 
} 

HTH、SiSの

+0

メインメソッドの外に別のメソッドを作成したことはありません。私が他のコードの前にこれを置くと、何か他のことが起こる前に、再びプレイするようにユーザーに頼むことはありませんか?私はちょうど私のコードに正確なフィットを得るためにこれを実装する方法を理解しようとしています –

+0

こんにちはクリストファー。 main()メソッドはプログラムのエントリポイントではありません。メインの1行目と2行目は "単なる変数宣言"です。 3行目は、あなたの要求された「再生を再開する」-Loopの開始点です。そして、4行目の "play()"では、あなたの新しい関数private static void play(){あなたの最初の投稿全体のループ}が実行されます。この時点で、プロンプトが表示されませんでした。最初のプロンプトには、play()の最初の呼び出しの後の5行目と6行目があります。 – StehtimSchilf

0

可能な解決策は、ゲームを配置することです別のwhileループ(gameCountのものを包み込む)とwhileループ内で、変数のswitch文を作成して、gameContinueとします。ゲームの終わりに、ユーザは続行するか否かを知らせるプロンプトを受け取る。彼らの答えに基づいて、あなたは変数gameContinueの値を変更してそこに行きます。

関連する問題