2017-02-25 23 views
-4

私はロックペーパーはさみプログラムを作成しています。最後に、私はすべての試合の優勝者を決定したい。最高のスコアを持つ人に関係なく、常に「Player1 beat Player2 and I」を印刷します。私の間違いはどこにありますか、どのように修正すればい​​いですか?一連のロックペーパーはさみゲームの総合優勝者

import java.util.Scanner; 
import java.util.Random; 

public class RPS2Player{ 
    public static void main(String[]args){ 
     int onePlay, computerPlay, twoPlay, game = 0, win1 = 0, win2 = 0, compWin = 0, lose1 = 0, lose2 = 0, compLose = 0, playChoice; 

     //instantiate objects Scanner and Random 
     Scanner UI = new Scanner(System.in); 
     Random num = new Random(); 

     //Get user input: Rock = 0, Paper = 1, Scissors = 2 
     System.out.println("Hello! would you like to play Rock, Paper, Scissors? 0=yes, 1=no"); 
     playChoice = UI.nextInt(2); 
     while (playChoice == 0){ 
     game++; 
     System.out.println("Round " + game); 
     System.out.println("Player1, choose 0=Rock, 1=Paper, 2=Scissors"); 
      onePlay = UI.nextInt(3); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println("Player2, choose =Rock, 1=Paper, 2=Scissors"); 
      twoPlay = UI.nextInt(3); 

     //get computer generated input: 0, 1, 2 
     computerPlay = num.nextInt(3);//defines a random choice of 0, 1, or 2 

     switch(onePlay){ 
      case 0: System.out.println("Player1 chose Rock"); break; 
      case 1: System.out.println("Player1 chose Paper"); break; 
      case 2: System.out.println("Player1 chose Scissors"); break; 
     }//end switch statement for onePlay 

     switch (twoPlay){ 
      case 0: System.out.println("Player2 chose Rock"); break; 
      case 1: System.out.println("Player2 chose Paper"); break; 
      case 2: System.out.println("Player2 chose Scissors"); break; 
     }//end switch statement for twoPlay 

     switch(computerPlay){ 
      case 0: System.out.println("I chose Rock"); break; 
      case 1: System.out.println("I chose Paper"); break; 
      case 2: System.out.println("I chose Scissors"); break; 
     }//end switch statement for computerPlay 

     //comparisons Player1 vs computer 
     if (onePlay == 0){//Rock 
      if (computerPlay == 0){ 
       System.out.println("Player 1 and I tie!"); 
       //computer chose rock - tie game 
      } 
      if (computerPlay == 1){ 
       System.out.println("I beat Player1!"); 
       lose1++; 
       compWin++;//computer chose paper - computer wins 
      } 
      if (computerPlay == 2){ 
       System.out.println("Player1 beat me!"); 
       compLose++; 
       win1++;//computer chose scissors - player wins 
      } 
     }//end if statements for onePlay = 0 (Rock) 

     if (onePlay == 1){//Paper 
      if (computerPlay == 0){ 
       System.out.println("Player1 beat me!"); 
       compLose++; 
       win1++;//computer chose rock - player win1s 
      } 
      if (computerPlay == 1){ 
       System.out.println("Player1 and I tie!"); 
       // computer chose paper - tie game 
      } 
      if (computerPlay ==2){ 
       System.out.println("I beat Player 1!"); 
       lose1++; 
       compWin++;//computer chose scissor - computer win1s 
      } 
     }//end if statements for onePlay = 1 (Paper) 

     if (onePlay == 2){//Scissors 
      if (computerPlay == 0){ 
       System.out.println("I beat Player1!"); 
       lose1++; 
       compWin++;//computer chose rock - computer wins 
      } 
      if (computerPlay == 1){ 
       System.out.println("Player1 beat me!"); 
       compLose++; 
       win1++;//computer chose paper - play wins 
      } 
      if (computerPlay == 2){ 
       System.out.println("Player1 and I tie!"); 
       //computer chose scissors - tie game 
      } 
     }//end if statements for onePlay = 2 (Scissors) 
     //end comparisons Player1 vs computer 

     //comparisons Player2 vs computer 
     if (twoPlay == 0){//Rock 
      if (computerPlay == 0){ 
       System.out.println("Player2 and I tie!"); 
       //computer chose rock - tie game 
      } 
      if (computerPlay == 1){ 
       System.out.println("I beat Player2!"); 
       lose2++; 
       compWin++;//computer chose paper - computer wins 
      } 
      if (computerPlay == 2){ 
       System.out.println("Player2 beat me!"); 
       compLose++; 
       win2++;//computer chose scissors - player2 wins 
      } 
     }//end if statements for twoPlay = 0 (Rock) 

     if (twoPlay == 1){//Paper 
      if (computerPlay == 0){ 
       System.out.println("Player2 beat me!"); 
       compLose++; 
       win2++;//computer chose rock - player wins 
      } 
      if (computerPlay == 1){ 
       System.out.println("Player2 and I tie!"); 
       // computer chose paper - tie game 
      } 
      if (computerPlay ==2){ 
       System.out.println("I beat Player2!"); 
       lose2++; 
       compWin++;//computer chose scissor - computer wins 
      } 
     }//end if statements for twoPlay = 1 (Paper) 

     if (twoPlay == 2){//Scissors 
      if (computerPlay == 0){ 
       System.out.println("I beat Player2!"); 
       lose2++; 
       compWin++;//computer chose rock - computer wins 
      } 
      if (computerPlay == 1){ 
       System.out.println("Player2 beat me!"); 
       compLose++; 
       win2++;//computer chose paper - player2 wins 
      } 
      if (computerPlay == 2){ 
       System.out.println("Player2 and I tie!"); 
       //computer chose scissors - tie game 
      } 
     }//end if statements for twoPlay = 2 (Scissors) 
     //end comparisons Player2 vs computer 

     //comparison Player1 vs Player2 
     if (onePlay == 0){//Rock 
      if (twoPlay == 0){ 
       System.out.println("Player1 and Player2 tie!"); 
       //Player2 chose rock - tie game 
      } 
      if (twoPlay == 1){ 
       System.out.println("Player2 beat Player1!"); 
       lose1++; 
       win2++;//Player2 chose paper - Player2 wins 
      } 
      if (twoPlay == 2){ 
       System.out.println("Player1 beat Player2!"); 
       lose2++; 
       win1++;//Player2 chose scissors - player1 wins 
      } 
     }//end if statements for personPlay = 0 (Rock) 

     if (onePlay == 1){//Paper 
      if (twoPlay == 0){ 
       System.out.println("Player1 beat Player2!"); 
       lose2++; 
       win1++;//computer chose rock - player wins 
      } 
      if (twoPlay == 1){ 
       System.out.println("Player1 and Player2 tie!"); 
       // computer chose paper - tie game 
      } 
      if (twoPlay ==2){ 
       System.out.println("Player2 beat Player1!"); 
       lose1++; 
       win2++;//player2 chose scissor - player2 wins 
      } 
     }//end if statements for personPlay = 1 (Paper) 

     if (onePlay == 2){//Scissors 
      if (twoPlay == 0){ 
       System.out.println("Player2 beat Player1!"); 
       lose1++; 
       win2++;//Player2 chose rock - player2 wins 
      } 
      if (twoPlay == 1){ 
       System.out.println("Player1 beat Player2!"); 
       lose2++; 
       win1++;//player2 chose paper - player1 wins 
      } 
      if (twoPlay == 2){ 
       System.out.println("Player1 and Player2 tie!"); 
       //player2 chose scissors - tie game 
      } 
     }//end if statements for personPlay = 2 (Scissors) 
     //end //comparisons Player1 vs Player2 
     System.out.println("Would you like to play again? 0=yes, 1=no"); 
     playChoice = UI.nextInt(2); 
     }//end while loop for play 

     //user decides not to play or decides to stop playing - Stats are given 
     if (playChoice == 1){ 
     System.out.println("Ok let's play (again) sometime.");//game ended 
     System.out.println(); 
     System.out.println("We played " + game + " games.");//number of games played 
     System.out.println("Player1 won " + win1 + " games, lost " + compWin + " games.");//Player1 wins and losses 
     System.out.println("Player2 won " + win2 + " games, lost " + lose2 + " games.");//Player2 wins and losses 
     System.out.println("I won " + compWin + " games and lost " + compLose + " games.");//computers wins and losses 
     System.out.print("In terms of wins, "); 

     //determine best player 
     System.out.println("Across all games played, "); 
     if (win1 > (win2 & compWin)){ 
     System.out.println("Player1 beat Player2 and I!"); 
     } 
     else if (win2 > (win1 & compWin)){ 
     System.out.println("Player2 beat Player1 and I!"); 
     } 
     else 
     System.out.println("I beat Player1 and Player2!"); 

     }//end while loop 
    }//end method 
}//end program 
+0

これは、「広がる」または「この形式では長い間良い回答が得られる」という理由で、これを保留にするのはなぜですか。私はすでに質問を編集して私の答えを得ました - 解決済み - –

答えて

1

あなたの条件は

if ((win1 > win2) && (win1 > compWin)){ 
    System.out.println("Player1 beat Player2 and I!"); 
    } 
    else if ((win2 > win1) && (win2 > compWin)){ 
    System.out.println("Player2 beat Player1 and I!"); 
    } 
    else 
    System.out.println("I beat Player1 and Player2!"); 

    } 

ためにあるべきとJavaでの声明は、 "& &" ではない "&です"、 条件チェックが間違っています

+0

ありがとうございます。私はこれを誰かに説明しなければならないなら、それを一つの条件に入れた二重比較でなければならないのですか? –

+0

win2とcompWinの両方とwin1を比較し、両方の比較結果を比較する必要があります。 –

1
if (win1 > (win2 & compWin)) 

これはelse ifため

if (win1 > win2 && win1 > compWin) 

と同じものでなければなりません。

論理AND演算子は&&です。 &はビット単位のANDであり、全く異なる演算です。それはあなたがまだ使用していないやや高度な機能です。

あなたが書いたことは、英語をコードに直接変換することができない一般的な間違いです。 &&||は、 "and"と "or"が英語で全く同じに動作しません。

(ちなみに、文法的には "Player1はPlayer2と私を破った。" と言うべきです)

+0

どうもありがとうございました。私はこれを説明しなければならないなら、それは一つの条件に入れられた二重比較でなければならないのですか? –

0

比較ではビット演算子を使用しています。私はそれがあなたが意図したものだとは思わない。

if (win1 > (win2 & compWin)){ 
    // if win2 = 14 and compWin = 9 
    //  9 (base 10) = 00000000000000000000000000001001 (base 2) 
    // 14 (base 10) = 00000000000000000000000000001110 (base 2) 
    //   -------------------------------- 
    //14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10) 
    // if win2 = 14 and compWin = 0; 14 & 0 = 0 
    System.out.println("Player1 beat Player2 and I!"); 
} 

私はあなたが論理的& &演算子を使用してWIN2と​​compWinにWIN1を比較するためのものだと思います。 & &かを使用して

// Should be: 
if(win1 > win2 && win1 > compWin) { 
    System.out.println("Player1 beat Player2 and I!"); 
} 
else if (win2 > win1 && win2 > compWin)){ 
    System.out.println("Player2 beat Player1 and I!"); 
} 
else 
    System.out.println("I beat Player1 and Player2!"); 
} 

論理式||一方は評価され、他方は評価される。左側が真の場合、& &右側が真、真が返されます。それ以外の場合はfalseを返します。

関連する問題