1

Javaでじゃんけんゲームを書いていますが、わからないことがいくつかあります。まず、ユーザーが 1、2、3 の代わりに「ジャンク」または「ペーパー」を入力できるようにしたいのですが、それがわかりません。第二に、ネストされた if else ステートメントを使用することになっていますが、これまで行ってきたことでそれを行う方法がわかりません。私のコードは以下です

java.util.Scanner をインポートします。public class RockGame {

  private final static int ROCK=1;
  private final static int PAPER =2;
  private final static int SCISSOR =3;

  private static Scanner key;




public static void main(String args[]){
         int playerOneScore = 0;
         int computerScore = 0;

         int userPlay, computerPlay;


        String val = key.nextLine().toLowerCase();

         key = new Scanner(System.in);
         while(playerOneScore <2 && computerScore <2){


                 System.out.println("Choose 1 for rock, 2 for paper, and 3 for sciscors!");
                 userPlay = key.nextInt();
                 computerPlay = (int)(Math.random()*3) +1;
                 if(val.equals("rock"))
                       userPlay = ROCK;

                 else if (val.equals("paper"))
                         userPlay =PAPER;

                 else if (val.equals("scissors"))
                         userPlay=SCISSOR;


                 if (val.equals("rock"))
                         computerPlay = ROCK;
                 else if (val.equals("paper"))
                         computerPlay =PAPER;
                 else if (val.equals("scissors"))
                         computerPlay=SCISSOR;

                 if (computerPlay ==ROCK && userPlay==SCISSOR ){
                         System.out.println("The computer chose rock, you chose scissors.\n You lose!");
                         computerScore++;
                 }
                 if (computerPlay ==ROCK && userPlay==PAPER ){
                         System.out.println("You computer chose rock, you chose paper.\n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==PAPER && userPlay==SCISSOR ){
                        System.out.println("The computer chose scissors, you chose paper.\n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==PAPER && userPlay==ROCK ){
                         System.out.println("The computer chose paper and you chose rock. \n You lose!");
                         computerScore++;
                 }
                 if (computerPlay ==SCISSOR && userPlay==ROCK ){
                         System.out.println("The computer chose scissors and you chose rock. \n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==SCISSOR && userPlay==PAPER ){
                         System.out.println("The computer chose scissors and you chose paper. \n You lose!");
                         computerScore++;
                 }
                 if (computerPlay == userPlay ){
                         System.out.println("The computer chose the same thing you did! \n Tie!");

                 }


         }
         if(computerScore > playerOneScore)
                 System.out.println("Computer win score is: - "+ computerScore + " -" + playerOneScore  );
         else
                 System.out.println("Your score is: " + playerOneScore + "-" + computerScore );


  }


}
4

9 に答える 9

4

ユーザーが 1、2、3 の代わりに「ジャンク」または「ペーパー」を入力できるようにしたい

を使用key.nextLine().toLower()して、この値が「岩」などと等しいかどうかをテストします。

ネストされた if else ステートメントを使用することになっています

あなたのコードに注意してください:

if (computerPlay ==SCISSOR && userPlay==ROCK ){
    // etc.
}
if (computerPlay ==SCISSOR && userPlay==PAPER ){
    // etc.
}

かどうかcomputerPlay == SCISSORを 2 回確認します。一方、ネストされたステートメントを使用すると、次のようなことができます。

if (computerPlay == SCISSOR) {
    if (userPlay == ROCK) {
        // etc.
    else if (userPlay == PAPER) {
        // etc.
    }
}
于 2013-09-19T01:31:02.217 に答える
1

これを試して

        // paper == 0
        // rock == 1
        // scissors == 2
        System.out.println("Select 0 for Paper \nSelect 1 for Rock \nSelect 2 for Scissors");
        Scanner scan = new Scanner(System.in);
        int player = scan.nextInt();
        Random comp = new Random();
        int com = comp.nextInt(2);
        System.out.println(com);
        if (player == com){
            System.out.println("Match is Tie");
        }else{
            switch (com){
                case 0:
                    if(player == 2){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
                case 1:
                    if(player == 0){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
                case 2:
                    if(player == 1){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
            }
        }
于 2021-07-25T03:05:01.407 に答える
0

これがあなたにできることだと少し考えた後、

要素を持つ列挙型を作成する

enum Elements{ROCK ,PAPER ,SCISSORS};

要素のさまざまな組み合わせの 2 番目の列挙型を作成し、インターフェイスも実装します。

enum ElementCombinations implements RockPaperScissorssLogic
{
    ROCK_PAPER
    {
        int decideWinner()
        {
            //For second player
            return 2;
        }
    }, 
    PAPER_SCISSORS
    {
        int decideWinner()
        {
            //For second player
            return 2;
        }
    }, 
    ..... ;

    // For obtaining decide the type of combination object to be used.
    public static ElementCombinations getCombination(Element player1, Element player2)
    {
        // Some logic...
    }
};

最後に、コード内で ElementCombinations オブジェクトの決定勝者メソッドを呼び出すだけで、勝者を取得できます。

于 2013-09-19T03:30:44.580 に答える