I'm trying to get a calculation done in a loop but don't want it to print until after all loop are done. It will print but the calculation isn't done. Please help.

import java.util.*;
public class FlipCoin
{
    public static void main(String[] args)
    {
    String choiceString;
    final String HEADS = "Heads";
                final String TAILS = "Tails";
    int coinToss = 1;
    double result;
    int heads = 1;
    int number = heads + 1;
    int percentage1 = number * 10;
    int percentage2 = 100 - percentage1;
    while(coinToss <= 10)
    {
        result = (Math.random());
        if(result <= .5)
    {
           choiceString = HEADS;
           heads = 1;
    }
        else
           choiceString = TAILS;
        System.out.println("Cointoss " + coinToss + " came up " + choiceString);
        coinToss++;
        number = heads + 1;
        }
        System.out.println("The percentage of tosses that were heads is " + percentage1 + "%" + 
                "\n& the percentage of tosses that were tails is " + percentage2 + "%");
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

at first you need to wrap your code using code tag like this

import java.util.*;
public class FlipCoin

i gone through your code you are printing if result is greater than .5 every time loop run. so if you dont want to print the result at the end place your print statement outside the loop.

hope it will help you.

I want it to print "heads" or "tails" at the end of each roll but I don't want the percentage to show until all 10 rolls are done. It show the sentence but I must have the calculations located wrong as it comes us 20% and 80% even when it should be higher and lower.

I want it to print "heads" or "tails" at the end of each roll but I don't want the percentage to show until all 10 rolls are done. It show the sentence but I must have the calculations located wrong as it comes us 20% and 80% even when it should be higher and lower.

here is the solution i hope this is what you want to achieve

import java.util.*;
public class FlipCoin
{
public static void main(String[] args)
{
String choiceString;
final String HEADS = "Heads";
final String TAILS = "Tails";
int coinToss = 1;
double result=0;
int heads = 0;
int number = heads;
int percentage1 = 0;
int percentage2 = 0;
while(coinToss <= 10)
{
result = (Math.random());
if(result <= .5)
{
choiceString = HEADS;
heads = heads+1; //counting number of heads here
}
else{
choiceString = TAILS; }

System.out.println("Cointoss " + coinToss + " came up " + choiceString);
coinToss++;
}
percentage1 = heads * 10;
percentage2 = 100 - percentage1;
System.out.println("The percentage of tosses that were heads is " + percentage1 + "%" + 
"\n& the percentage of tosses that were tails is " + percentage2 + "%");
}
}

what i did in your code is, i dont need "number" variable. and i Initialize percentage1 and 2 after loop is completed because i want to count the head. so every time when i run the loop i m counting head and at the end calculate percentage and print them.

hope it works with you.

This question has already been answered. Start a new discussion instead.