coinFlip头条纹输出

时间:2018-02-28 20:30:38

标签: java

我应该将硬币翻转100次,然后找出最长的条纹,然后输出它,但到目前为止,它只给了我总共有多少个头。我已经尝试了很多东西,但找不到解决方案。

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

    public void run()
    {
        int h = 0;
        int t = 0;
        boolean wasHeads = false;
        boolean isHeads = false;
        int streak = 0;
        int ih = 0;

        for (int i = 0; i < FLIPS; i++) {
            int coinFlip = Randomizer.nextInt(1, 2);

            if (coinFlip == 2) {
                System.out.println("Heads");
                h++;
                ih++;
                isHeads = true;

                if (ih > 1) {
                    wasHeads = true;
                }
                if ((isHeads = true) && (wasHeads = true)) {   
                    streak++;
                } else {
                    streak = 0;
                }
            } else if (coinFlip == 1) {
                System.out.println("Tails");
                t++;  
                isHeads = false;
                ih = 0;
            }
        }

        System.out.println(streak);
    }
}

4 个答案:

答案 0 :(得分:0)

您需要大幅简化代码。

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

    public void run()
    {
        int wasHeads = 0;
        int streak = 0;
        int bestStreak = 0;

        for (int i = 0; i < FLIPS; i++) {
            int coinFlip = Randomizer.nextInt(1, 2);

            if (coinFlip == 2) {
                streak++;
                wasHeads = 1;
            } else if (coinFlip == 1) {
                wasHeads = 0;
                if (steak > bestStreak) {
                    bestStreak = streak;
                }
                streak = 0;
            }
        }
        System.out.println(streak);
    }
}

结束时重置条纹(coinFlip为1),但首先检查它是否比之前的最佳条纹更好,如果相应则更新bestStreak。否则,只需迭代条纹并将每个头部的wasHeads保持为1。注意,这里不需要布尔值; 1或0整数在字面上和逻辑上是相同的。

答案 1 :(得分:0)

首先,请在此处修改您的行,以便更容易阅读:}} else if (coinFlip == 1) {
(你需要在这两个紧密的大括号之间换行)

我会考虑设置一些更清晰的变量名。例如:totalHeadstotalTails。我不太了解你的ih变量是什么,但我用来解决这个问题的变量名是thisStreakbestStreak。是/ washeads应该是不必要的。我认为这应该足以让你找到解决方案。

答案 2 :(得分:0)

您可以稍微简化一下代码,并不真正需要wasHeadsisHeads。此外,更具描述性的变量名称使您的代码更容易理解。所以,像这样:

public void run() {
    int highestNumberOfConsecutiveHeads = 0;
    int currentNumberOfConsecutiveHeads = 0;
    for (int i = 0; i < 100; i++) {
        if (Randomizer.nextInt(1, 2) == 2) {
            currentNumberOfConsecutiveHeads++;
            if (currentNumberOfConsecutiveHeads > largestNumberOfConsecutiveHeads) {
                highestNumberOfConsecutiveHeads = currentNumberOfConsecutiveHeads;
            }
        } else {
            currentNumberOfConsecutiveHeads = 0;
        }
    }
    System.out.println("Longest streak: " + highestnumberOfConsecutiveHeads);
}

答案 3 :(得分:0)

感谢您的帮助,修复了我的代码:)

<table class="table table-striped table-bordered table-hover" id="sample_1">

                    @foreach (var item in Model.genelParametreler)
                    {
                        <tr>
                            <td>@item.ParametreAdi</td>
                            <td>@item.Deger</td>
                            <td>@item.Aciklama</td>
                            <td><a onclick="location.href = '/Parametre/GenelParamDuzenle?ParametreID=@item.ParametreID';" style="cursor:pointer"> <span class="fa fa-pencil-square-o"></span> Güncelle</a></td>
                            <td><a id="@item.ParametreID" onclick="parametreSil(@item.ParametreID)" style="cursor:pointer; color:red"><span class="fa fa-times" style="color:red"></span>Sil</a></td>
                        </tr>
                    }

            </table>

{     public static final int FLIPS = 100;

public class LongestStreak extends ConsoleProgram

}