2013-10-31 25 views
6

ご挨拶スタックオーバーフローのユーザー、私は作成したJavaプログラムの援助のために、今晩あなたに来ています。私はJavaには比較的新しいので、このトピックに関する私の無知を許してください。私は "Rock" "Paper" "Scissors"ゲームのJavaプログラムを作成しましたが、ステートメントの1つにエラーがあるようです。JavaネストされたWhileループ

import java.util.Scanner; 

public class TheAntlers { 
public static void main(String[] args) { 

    int playerHumanWins = 0; 
    int playerComputerWins = 0; 
    int numberOfTies = 0; 
    int computerResult; 

    Scanner input = new Scanner(System.in); 

    while(true) { 

     String startGame; 
     String playerHuman; 
     String playerComputer = " "; 

     System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): "); 
     startGame = input.nextLine(); 

     startGame = startGame.toUpperCase(); 

     if(startGame.equals("N")) { 
      System.out.println("NO!"); 
      break; 
     } 
     else if(! startGame.equals("Y")) { 
      startGame = startGame.toLowerCase(); 
      System.out.println("Sorry, " + startGame + " is not a valid entry...");    
     } 
     while(startGame.equals("Y")) { 
      System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": "); 
      playerHuman = input.nextLine(); 

      computerResult = (int)(Math.random() * 3); 

      playerHuman = playerHuman.toUpperCase(); 

      if(computerResult == 1) { 
       playerComputer = "ROCK"; 
      } 
      else if(computerResult == 2) { 
       playerComputer = "PAPER"; 
      } 
      else if (computerResult == 3) { 
       playerComputer = "SCISSORS"; 
      } 

      switch (playerHuman) { 
       case "ROCK" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"ROCK\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("PAPER")) { 
         System.out.println("Computer wins!"); 
         playerComputerWins++; 
        } 
        else { 
         System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        break; 
       case "PAPER" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"PAPER\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("ROCK")) { 
         System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        else { 
         System.out.println("Sorry, the computer won!"); 
         playerComputerWins++; 
        } 
        break; 
       case "SCISSORS" : 
        if(playerComputer.equals(playerHuman)) { 
        System.out.println("Tie you both picked \"SCISSORS\""); 
        numberOfTies++; 
       } 
        else if(playerComputer.equals("PAPER")) { 
         System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\""); 
         playerHumanWins++; 
         return; 
        } 
        else { 
         System.out.println("Sorry, the computer won!"); 
         playerComputerWins++; 
        } 
        break; 
       default: 
        playerHuman = playerHuman.toLowerCase(); 
        System.out.println("Sorry, " + playerHuman + " is not a valid entry..."); 
        break; 
      } 
     } 
    }   
} 
} 

私が直面している問題は、勝利計算に関連しています。私は、プログラムを実行すると、私は岩を入力し、繰り返し私が勝つまで、出力はあなたが勝つになり、「ROCK」は「ビート」をが、他のオプションを使用して、私はあなたが勝つを取得し、「ROCKはPAPER」

を「打ちます」

私の質問は、なぜロックを再生するときに空のコールバックを取得するのですか?

* また、初心者を助ける他の提案があればそれを喜んで指摘したいと思うなら。 *

答えて

5

Math.random() * 3、それは0、1、または2

 if(computerResult == 0) { 
      playerComputer = "ROCK"; 
     } 
     else if(computerResult == 1) { 
      playerComputer = "PAPER"; 
     } 
     else if (computerResult == 2) { 
      playerComputer = "SCISSORS"; 
     } 

提案番号INTにキャストした後、少なくとも0および3未満

である:

簡潔にする。あなたがスクロールしてスクロールする必要がないときにそれをより読みだ

String startGame = input.nextLine().toUpperCase(); 

String startGame; 
startGame = input.nextLine(); 
startGame = startGame.toUpperCase(); 

を変更することができます。

また、equalsIgnoreCase()が存在することをご存知でしょうか。

+0

(computerResult == 0){ playerComputer = "ROCK"; } else if(computerResult == 1){ playerComputer = "PAPER"; } else { playerComputer = "SCISSORS"; } –

+2

1から3までの数字が必要な場合は、次のように追加することを忘れないでください。1 +(int)Math.random()* 3; –

+0

良い点@AmirAfghani、この場合、0,1,2はほとんどのプログラマーにとって自然です。 –

0

0の結果がないので、(int)(Math.random()* 3)が0になり、決して3になりません。空白を返します。

具体的には、そのMath.random()が返す未満0.33

1

これは完全な初心者のためではありませんが、私はこのコード使用してゲームをモデル化します:

enum Choice { ROCK, PAPER, SCISSORS } 

enum Result { COMPUTER_WINS, TIE, HUMAN_WINS } 

Result decide(Choice computer, Choice human) { 
    if (human == computer) { 
    return Result.TIE; 
    } else if (…) { 
    … 
    } 
} 

あなたには、いくつかを持ってその方法をゲームそのものを処理するコードの一部ですが、他のコードではユーザーのやりとりを処理します。

関連する問題