2017-11-02 5 views
-3

ダブルプログラムがロールバックされるまで(2回は同じ)3回ダイスを繰り返してロールバックする必要があります。毎回値を表示し、その後にダブルスを取得するのに必要な試行回数を指定します。Eclipse Java Loopsダイスロールを2倍にして何回実行したかを表示

問題はそれが永遠に実行され、何回実行されたか正確にはわかりません。

これまで私がこれまで持っていたことは次のとおりです。

public class ExtraProblem4 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int first, second, third; 
     int counter = 0; 
     while(true) { 
      counter ++; 

      first = (int)(Math.random()*(6-1+1)+1); 
      second = (int)(Math.random()*(6-1+1)+1); 
      third = (int)(Math.random()*(6-1+1)+1); 

      System.out.println(first + "\t" + second + "\t" + third + "\t"); 

      if (first == second || second == third || first == third) { 
       System.out.println("Double! It took " + counter + " tries to get it!"); 
      } 
      else { 
       first = (int)(Math.random()*(6-1+1)+1); 
       second = (int)(Math.random()*(6-1+1)+1); 
       third = (int)(Math.random()*(6-1+1)+1); 

       System.out.println(first + "\t" + second + "\t" + third + "\t"); 
      } 
     } 

    } 
+5

''問題は永遠に実行されています... " - これは' while(true) 'と少し関係があります。 –

+1

あなたはそのelseブロック全体を取り除く方が良いかもしれないと思います。あなたがしていないことは何もしていません。 –

+0

このコードではデバッグの証拠がないので、私はこの質問をd​​ownvotedしました。あなたの質問を編集して、あなたのデバッグが明らかにしたことと、特定のコード行に関する特定の質問を表示してください。参照:[最小限で完全で検証可能な例の作成方法](http://stackoverflow.com/help/mcve)と[小規模プログラムのデバッグ方法](https://ericlippert.com/2014/03/05)/how-to-debug-small-programs /)を実行します。 –

答えて

-1
public class ExtraProblem4 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int first, second, third; 
     int counter = 0; 
     boolean isDoubles= false; 
     while (!isDoubles) 
     { 
      first = (int)(Math.random()*(6-1+1)+1); 
      second = (int)(Math.random()*(6-1+1)+1); 
      third = (int)(Math.random()*(6-1+1)+1); 

      System.out.println(first + "\t" + second + "\t" + third + "\t"); 
      isDoubles= first == second || first == third || second == third; 
      counter++; 
     }  
     System.out.println("Double! It took " + counter + " tries to get it!"); 
     } 
    } 
0

かわりに、DO-しばらく使用して...また、私は場合で、他のように、コードのいくつかの冗長ラインを処分したほうが良いかもしれません。

public class ExtraProblem4 
{ 

    public static void main(String[] args) 
    { 
     // TODO Auto-generated method stub 
     int first, second, third; 
     int counter = 0; 
     boolean rolledDoubles; //initializes boolean variable to keep the loop going 
     do //use a do-while so it tests after each roll 
     { 
      rolledDoubles = false; //defines variable as false so it keeps looping 
      counter = counter + 1; // more reliable step 

      first = (int)(Math.random()*(6-1+1)+1); 
      second = (int)(Math.random()*(6-1+1)+1); 
      third = (int)(Math.random()*(6-1+1)+1); 

      System.out.println(first + "\t" + second + "\t" + third + "\t"); 

      if (first == second || second == third || first == third) 
      { 
       rolledDoubles = true; //you rolled a double! This will stop the loop 
       System.out.println("Double! It took " + counter + " tries to get it!"); 
      } 

     } 
     while(!rolledDoubles); //uses not operator with rolledDoubles so when rolledDoubles is true then we stop the loop, if false we keep going 
    } 

} 
関連する問題