2016-05-12 3 views
-1

私のクラスでは、私はサイコロのゲームを作る必要があります、私は戦争のバージョンを作った、私は少し助けを完了する必要があります。私は最後のポイントを表示する必要があり、何とか私はスコアの違いを表示することができたら欲しいです。私はまた、ループオーバーでゲームを終了する方法を知らない。私はゲームを終了するか、または続行する必要があります。ループwhileクラスのためのループ

public class Game { 

    public static void main(String[] args) 
     { 
     Dice myDie = new Dice(); 
     Dice CompDie= new Dice(); 

     int player =0; 
     int computer =0; 
     boolean gameOver; 
     do 
     { 
      int[] scores = new int[3]; 
      gameOver = false; 
      while(!gameOver) 
      { 
       myDie.roll(); 
       System.out.println("You rolled a " + myDie.getValue()); 
       player = myDie.getValue(); 

       CompDie.roll(); 
       System.out.println("Computer rolled a " + CompDie.getValue()); 
       computer = CompDie.getValue(); 

       checkResults(player, computer, scores); 
       printResults(scores); 

       // ask player if want to continue enter Y to continue 
       System.out.println("Do you want to continue playing enter Y if so"); // I need to end loop here 




      } 

     }while(keepPlaying()); 








     } 
    public static boolean keepPlaying() 
    { 
     Scanner readIn = new Scanner(System.in); 
     boolean playAgain = false; 
     System.out.println("Do you want to play again?"); 
     String answer = readIn.nextLine().toUpperCase(); 
     char ans = answer.charAt(0); 
     if(ans == 'Y') 
      playAgain = true; 
     return playAgain; 

    } 
    public static void checkResults(int player, int computer, int[] scores) 
    { 
     if(player > computer) 
     { 
      scores[1]++; 
     } 
     else if(player < computer) 
     { 
      scores[2]++; 
     } 
     else 
     { 
      scores[0]++; 
     } 
    } 
    public static void printResults(int[] list) 
    { 
     System.out.println("  Ties Player Computer"); 
     for (int i = 0; i < list.length; i++) 
     { 
      System.out.printf("%8d", list[i]); 
     } 
     System.out.println(); 
    } 
} 
+0

最も内側のループは完全に不要です。それを取り除き、その内部のコードを最初のループの中に入れるだけです。また、最初のループの前に 'scores'を初期化する必要があります。そうでなければ、ループが実行されるたびにブランク配列として再作成され、以前に追加された値は保持されません。 –

+0

この質問はIDEとは関係がないので、これを 'eclipse'とタグ付けする必要はありません。 –

+0

私はループを終了しましたが、あなたが言ったように初期化する必要があります。どうすればいいですか? – Lucas

答えて

0
while(!gameOver) 
     { 

     } 

このループは不要であり、また停止することはありません。それでもやりたい場合は、終了するときにgameOver = trueを設定する必要があります。

スコアの差を表示すると、スコアを差し引いて印刷するだけです。 System.out.println(Math.abs(score[1] - score[2])); このようなものです。

+0

しかし、ユーザーがYキーを押して続行するときに入力をどのように取ることができますか。 – Lucas

関連する問題