1

我正在处理一个硬币翻转任务,并且我的大部分工作都正常运行(尽管与我在这里看到的代码相比,它的方式不太优雅)。

我试图找到一种方法来告诉用户哪个数字出现在他们的翻转中最多,如果正面分配给偶数#s,而反面分配给奇数#s,哪个出现最多。我正在寻找实现这些功能的建议。

这是到目前为止的代码:

import java.io.*;
import java.util.*;


public class coinFlip {

    public static void main(String[] args) throws IOException {

        // declare in as a BufferedReader; used to gain input from the user
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));

        //declare variables
        int flips;
        int anArray[];
        int x;
        int r;
        int counter1 = 0;
        int counter2 = 0;
        int counter3 = 0;
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0;
        int counter7 = 0;
        int counter8 = 0;
        int counter9 = 0;
        int counter10 = 0;


            System.out.println("How many times would you like to flip your coin?");
            flips = Integer.parseInt(in.readLine());

            if (flips > 1000) {
            System.out.println("Invalid input, restart program!");
            }

            if(flips <= 1000) {
                System.out.println("You want to flip " + flips + " times");
                anArray = new int[flips];

                for(x = 0; x < flips; x++) {
                    r = (int) Math.round(Math.random()*9)+1;
                    anArray[x] = r;
                    System.out.println(anArray[x]);

                    if (anArray[x] == 1) {
                    counter1 += 1;
                    }
                    else if (anArray[x] == 2) {
                    counter2 += 1;
                    }
                    else if (anArray[x] == 3) {
                    counter3 += 1;
                    }
                    else if (anArray[x] == 4) {
                    counter4 += 1;
                    }
                    else if (anArray[x] == 5) {
                    counter5 += 1;
                    }
                    else if (anArray[x] == 6) {
                    counter6 += 1;
                    }
                    else if (anArray[x] == 7) {
                    counter7 += 1;
                    }
                    else if (anArray[x] == 8) {
                    counter8 += 1;
                    }
                    else if (anArray[x] == 9) {
                    counter9 += 1;
                    }
                    else if (anArray[x] == 10) {
                    counter10 += 1;
                    }
            }

            System.out.println("\n You rolled 1 " + counter1 + " times.");
            System.out.println("You rolled 2 " + counter2 + " times.");
            System.out.println("You rolled 3 " + counter3 + " times.");
            System.out.println("You rolled 4 " + counter4 + " times.");
            System.out.println("You rolled 5 " + counter5 + " times.");
            System.out.println("You rolled 6 " + counter6 + " times.");
            System.out.println("You rolled 7 " + counter7 + " times.");
            System.out.println("You rolled 8 " + counter8 + " times.");
            System.out.println("You rolled 9 " + counter9 + " times.");
            System.out.println("You rolled 10 " + counter10 + " times.");



        }
    }
 }
4

3 回答 3

4
import java.io.*;
import java.util.Random;

public class CoinFlip {
    public static void main(final String[] args) throws IOException {

        // declare in as a BufferedReader; used to gain input from the user
        final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        //declare variables
        System.out.println("How many times would you like to flip your coin?");
        final int flips = Integer.parseInt(in.readLine());;

        if (flips > 1000) {
            System.out.println("Invalid input, restart program!");
            return;
        }

        System.out.println("You want to flip " + flips + " times");
        final int[] counters = new int[10],
                     side     = new int[2];

        int r=0,x,max=0;
        final Random rand = new Random();

        for(x = 0; x < flips; ++x) {
            r = rand.nextInt(10);
            System.out.println(r+1);
            counters[r]++;
        }

        for ( x = 0; x < counters.length; ++x )
        {
            System.out.println("You rolled " + (x+1) + " " + counters[x] + " times.");
            if ( counters[x] > max )
            {
                max = counters[x];
                r = x+1;
            }
            side[x%2] += counters[x];
        }

        System.out.println(r + " was rolled most.");
        System.out.println("You rolled " + side[0] + " heads and " + side[1] + " tails." );
    }
 }
于 2014-01-28T01:02:07.770 回答
3

如另一个答案所示,使用循环将使生活变得更加轻松。

您的代码中有一个更严重的逻辑错误:

将 的输出四舍五入Math.random(),得到一个介于 0 和 1 之间的浮点数,然后四舍五入得到一个整数。下表显示了您将从代码中获得的与 RNG 相关的输出:

| Math.random() output | Resulting Integer |
| 0    ~ 0.04999       | 0                 |
| 0.05 ~ 0.14999       | 1                 |
| ......               | .....             |
| 0.95 ~ 0.99999       | 10                |

看到问题了吗?0 和 10 出现的次数只有其他数字的一半。

您应该要么floor()输出,要么使用Random.nextInt()生成整数的均匀分布。

于 2014-01-28T00:55:11.690 回答
2

这将使您的生活更轻松:

int counter[10];

...

for(x = 0; x < flips; x++) {
    r = (int) Math.round(Math.random()*9)+1;
    anArray[x] = r;
    System.out.println(anArray[x]);
    counter[r-1]++;
}

for(i=1; i <= counter.length; i++)
    System.out.println("You rolled " + i + " " + counter[i-1] + " times.");

对于您的其他两个问题,没有什么可以真正“建议”的。只需遍历counter,记住最大值,然后分别将偶数和奇数索引相加。

于 2014-01-28T00:41:18.107 回答