Java计算最长可能的数组

标签 java arrays count

我正在做一个抛硬币程序,并试图确定抛出的正面或反面的最长可能运行次数。我已经有了用于确定抛掷是正面还是反面的代码,但现在需要计算最长的可能运行时间。帮助!这是我的基本程序代码。

    public static void coin_toss(char [] toss)
    {
        int s = 0;
        try
        {
            for (s = 0; s <= toss.length; s++)
            {
                double flip;
                flip = (double)(Math.random());
                if (flip < 0.5)
                    toss[s] = 't';
                else
                    toss[s] = 'h';
            }//end of for loop to load array
        }
        catch (ArrayIndexOutOfBoundsException errorMessage)
        {
            System.out.println("\nSubscript out of bounds");
            System.out.println("Subscript went past the limit of " + toss.length);
            System.out.println("Last value of subscript was --> " + s);
            System.out.println("-------------------------------------------------");
            System.out.println(errorMessage);
            System.out.println("-------------------------------------------------");
        }
    }//end of toss coin

    public static double percent_heads (char [] toss)
    {
        double percent_h;
        int heads = 0;
        for (int s = 0; s < toss.length; s++)
        {
            if (toss[s] == 'h')
            heads = heads + 1;
        }
        System.out.println("There were " + heads + " heads results");
        percent_h = (double)heads / toss.length;
        return (percent_h);
    }//end of heads percentage function

    public static double percent_tails (char [] toss)
    {
        double percent_t;
        int tails = 0;
        for (int s = 0; s < toss.length; s++)
        {
            if (toss[s] == 't')
            tails = tails + 1;
        }
        System.out.println("There were " + tails + " tails results");
        percent_t = (double)tails / toss.length;
        return (percent_t);
    }//end of tails percentage function

    public static void main(String [] args)
    {
        int num_toss = 0;
        double heads, tails;
        double percent_t, percent_h;
        DecimalFormat percent = new DecimalFormat ("#0.00%");

        System.out.print("How many tosses would you like? --> ");
        num_toss = GetInput.readLineInt();
        char [] toss = new char[num_toss];

        System.out.println("You chose " + toss.length + " tosses");
        coin_toss(toss);
        heads = percent_heads(toss);
        tails = percent_tails(toss);
        System.out.println("The percentage of heads was --> " + percent.format(heads));
        System.out.println("The percentage of tails was --> " + percent.format(tails));
        longest_toss(toss);
        java.util.Date today = new java.util.Date();
        System.out.println("\nProgram terminated at " + today);
        System.exit(0);
    }//end of main method
}//end of class

最佳答案

我想出了一个方法。

public static void longest_toss(char[] toss){
        int longestrun = 0;
        int curlongestrun = 0;
        char prevrun = toss[0];

         for (int s = 1; s < toss.length; s++)
         {
             if (toss[s] == prevrun) {
                 curlongestrun++;
             }else {
                 curlongestrun=0;
             }
             if(curlongestrun>longestrun){
                 longestrun = curlongestrun;
             }
             prevrun = toss[s];
         }
         System.out.println("Longest run is : " + longestrun + " Coin side : " + prevrun);
    }

关于Java计算最长可能的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20416241/

相关文章:

c - C语言中如何分配不同类型的内存?

postgresql - Postgres : Getting a total related count based on a condition from a related table

MySql:同一张表上的硬匹配

java - 如何让 Hibernate 3 仅在调用其 getter 时检索值

php - 如何拆分数组以将其插入另一个数组以进行 SQL 查询?

java - Cardview 圆角没有显示在屏幕截图中?

javascript - 获取未匹配到单个数组的数组元素的结果

MySQL Avg Count With Where 和 Group By

java - native react :android.view.WindowManager $BadTokenException

java - 文字转语音时出错 : "Only the original thread that created a view hierarchy can touch its views."