2017-03-21 5 views
0

私は数週間これについてきましたが、どうやってこれを行うのか分かりません。私はちょうど最長の頭が欲しい。私は無数の時間を試して、私は間違って何をしているのか分かりません。最長ストリークを数えます

public class LongestStreak extends ConsoleProgram 
{ 
public static final int FLIPS = 100; 
int currentRun; 
int runStart; 
int maxRun; 

public void run() 
{ 
double heads = 0; 
double tails = 0; 
    for(int i = 0; i < FLIPS; i++) 
     { 
     if(Randomizer.nextBoolean()) 
     { 
      System.out.println("Heads"); 
      heads++; 
      currentRun++; // use ++ in preference to +=1, and this should be before maxRun test 
      if (maxRun < currentRun) { 
       maxRun = currentRun; 
       runStart = currentRun - i; // this will produce a 1-based position 
      } else { 
      currentRun = 0; 
      } 
     } 
     else 
     { 
      System.out.println("Tails"); 
      tails++; 
     } 
    } 
    System.out.println(FLIPS); 
} 

}

+1

を反転しますあなたはストリークを決して印刷しません。代わりに、ループの境界を表示します。 –

+1

テールが反転されている場合、currentRunを0にリセットすることを検討する必要があります。 – Compass

+0

ああ、私はmaxRunを印刷するように変更しましたが、maxRunはちょうどすべてのヘッドのようです。 100枚のフリップのように、51頭がいました。私はそれを望んでいない。 –

答えて

0

私はあなたの問題の権利を理解している場合、私はわからないが、ここで私は100の中にあなたの最長連勝を印刷するであろう、解決策を持っているが

public static void run() { 
    Random r = new Random(); 
    double heads = 0; 
    double tails = 0; 
    for (int i = 0; i < FLIPS; i++) { 
     if (r.nextBoolean()) { 
      System.out.println("Heads"); 
      heads++; 
      currentRun++; 
      if (maxRun < currentRun) { 
       maxRun = currentRun; 
       runStart = currentRun - i; 
      } 
     } else { 
      System.out.println("Tails"); 
      tails++; 
      currentRun = 0; 
     } 
    } 
    System.out.println(FLIPS); 
    System.out.println(maxRun); 
} 
+0

私はそれを追加しましたが動作しません –

+0

誰も知らない? –

+0

@AZGames何がうまくいかない? – XtremeBaumer

0
public class LongestStreak extends ConsoleProgram 
{ 
    public static final int FLIPS = 100; 


    public void run() 
    { 

     int consecutiveHeads = 0; 
     int maxRun = 0; 

     for(int i = 0; i < FLIPS; i++) 
     { 
      if(Randomizer.nextBoolean()) 
      { 
       System.out.println("Heads"); 
       consecutiveHeads++; 
        if(maxRun < consecutiveHeads) 
        { 
         maxRun = consecutiveHeads; 
        } 


      } 
      else 
      { 
       System.out.println("Tails"); 
       consecutiveHeads = 0; 
      } 
     } 

         System.out.println("Longest streak of heads: " + maxRun); 
    } 
} 
+1

Plsはこのコードがどのように問題を解決するかを説明します。 –

関連する問題