質問

I'm working on a coin flip assignment and I have the majority of it functioning properly (albeit in a less elegant fashion compared to the code I see on here).

I'm trying to find a way to tell the user which number appears most in their flips, and if heads are assigned to even #s, and tails to odd #s, which one came up the most. I'm looking for suggestions to implement these features.

Here is the code thus far:

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.");



        }
    }
 }
役に立ちましたか?

解決

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." );
    }
 }

他のヒント

Using loops, as shown in another answer, will make life much easier.

There is a more critical logic error in your code:

You round the output of Math.random(), which gives a float between 0 and 1, and round that to get an integer. The following table shows what output you'll get from your code in respect to the RNG:

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

See the problem? 0 and 10 appear only half as much as the other numbers.

You should either floor() the output, or use Random.nextInt() to generate a uniform distribution of ints.

This would make your life much easier:

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.");

For your other two problems, there's nothing to really "suggest". Just iterate over counter, remember the largest value, and add up the even and odd indices separately.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top