vendredi 28 octobre 2016

Trying to get percentage but keeps coming out 0 [duplicate]

This question already has an answer here:

I've been trying to figure this out for about 3 hours and I am finally almost done; I just need help with this last part. The problem is as follows: "Write a Java program that simulates the random flipping of two coins. At the start of the program prompt the user for the amount of times to flip the coin. Display each flip, and afterward display the percentages of heads and tails for each coin. Also, display the percentage that both coins were heads, and the percentage both coins were tails." I can't seem to figure out why I keep getting 0's for percentages on all of them.

import java.util.Scanner;
public class prob3
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("How many times would you like to flip the coins?");
        int n;
        n = in.nextInt();
        int coin1HeadsCount = 0;
        int coin2HeadsCount = 0;
        int coin1TailsCount = 0;
        int coin2TailsCount = 0;
        for (int i = 1; i <= n; i++)
        {
            int range = 2;
            int randomNum = (int) (range * Math.random()) + 1;
            int range2 = 2;
            int randomNum2 = (int) (range2 * Math.random()) + 1;
            if (randomNum == 1 &&
            randomNum2 == 1)
            {
                System.out.println("H H");
                coin1HeadsCount++;
                coin2HeadsCount++;
            }
            else if (randomNum == 1 &&
            randomNum2 == 2)
            {
                System.out.println("H T");
                coin1HeadsCount++;
                coin2TailsCount++;
            }
            else if (randomNum == 2 &&
            randomNum2 == 2)
            {
                System.out.println("T T");
                coin1TailsCount++;
                coin2TailsCount++;
            }
            else if (randomNum == 2 &&
            randomNum2 == 1)
            {                
                System.out.println("T H");
                coin1TailsCount++;
                coin2HeadsCount++;                
            }        
        }
        int total1Heads = (coin1HeadsCount / n);
        int total2Heads = (coin2HeadsCount / n);
        int total1Tails = (coin1TailsCount / n);
        int total2Tails = (coin2TailsCount / n);
        int bothHeads = ((coin1HeadsCount + coin2HeadsCount) / n);
        int bothTails = ((coin1TailsCount + coin2TailsCount) / n);
        System.out.println("Coin 1 was heads " + total1Heads + ", and tails " + total1Tails);
        System.out.println("Coin 2 was heads " + total2Heads + ", and tails " + total2Tails);
        System.out.println("The percentage both coins were heads was " + bothHeads);
        System.out.println("The percentage both coins were tails was " + bothTails);
    }
}

Aucun commentaire:

Enregistrer un commentaire