2017-03-11 20 views
-1

私はロックのために1,2,3を選んだ岩、紙、はさみのゲームを作ろうとして、あなたが選んだものとコンピュータが選んだものを表示します。その後、勝利のスコアを記録しておく必要があります。また、すべてのラウンドの最後に、再度プレイしたいかどうかをユーザーに尋ねます。ロックペーパーはさみ難しいJava

 import java.util.Scanner; 

    public class Rock5 
    { 
    //----------------------------------------------------------------- 
    // Plays the Rock-Paper-Scissors game with the user. 
    //----------------------------------------------------------------- 
    public static void main (String[] args) 
    { 
    final int OPTIONS = 3; 
    final int ROCK = 1, PAPER = 2, SCISSORS = 3; 
    final int COMPUTER = 1, PLAYER = 2, TIE = 3; 

    int computer, player, winner = 0; 
    int wins = 0, losses = 0, ties = 0; 
    String again; 
    Scanner in = new Scanner(System.in); 
    do 
    { 
    computer = (int) (Math.random() * OPTIONS) + 1; 

    System.out.println(); 
    System.out.print ("Enter your choice - 1 for Rock, 2 for " + 
          "Paper, and 3 for Scissors: "); 
    player = in.nextInt(); 

    System.out.print ("My choice was "); 

    // Determine the winner 
    switch (computer) 
    { 
     case ROCK: 
      System.out.println ("Rock."); 
      if (player == SCISSORS) 
       winner = COMPUTER; 
      else 
       if (player == PAPER) 
       winner = PLAYER; 
       else 
       winner = TIE; 
      break; 

     case PAPER: 
      System.out.println ("Paper."); 
      if (player == ROCK) 
       winner = COMPUTER; 
      else 
       if (player == SCISSORS) 
       winner = PLAYER; 
       else 
       winner = TIE; 
      break; 

     case SCISSORS: 
      System.out.println ("Scissors."); 
      if (player == PAPER) 
       winner = COMPUTER; 
      else 
       if (player == ROCK) 
       winner = PLAYER; 
       else 
       winner = TIE; 
    } 

    // Print results and increment appropriate counter 
    if (winner == COMPUTER) 
    { 
     System.out.println ("I win!"); 
     losses++; 
    } 
    else 
     if (winner == PLAYER) 
     { 
      System.out.println ("You win!"); 
      wins++; 
     } 
     else 
     { 
      System.out.println ("We tied!"); 
      ties++; 
     } 

    System.out.println(); 
    System.out.print ("Play again (y/n)?: "); 
    again = in.nextLine(); 
    } 
    while (again.equalsIgnoreCase ("y")); 

    // Print final results 
    System.out.println(); 
    System.out.println ("You won " + wins + " times."); 
    System.out.println ("You lost " + losses + " times."); 
    System.out.println ("We tied " + ties + " times."); 

} }

+4

新しい自転車が必要です。今あなたの質問は何ですか? –

+3

また、JavaとJavascriptは同じものではありません。彼らはカーとカーペットに似ています。 –

+2

なぜユーザーが再度プレイしたいかを尋ねるプログラムはありませんか? –

答えて

1

使用

player = Integer.valueOf(in.nextLine()); 

代わりの

player = in.nextInt(); 

とされていない場合、あなたが入力した番号がplayerに入るので、それは、うまくいく、と\ n characaterはagain = in.nextLine()に入り、それはとは異なるのでが停止します。

関連する問題