Jump to content

rogue482

Members
  • Content count

    5
  • Joined

  • Last visited

About rogue482

  • Rank
    New Member
  1. rogue482

    Coin Flip Percentages

    Thanks, that fixed it.
  2. rogue482

    Coin Flip Percentages

    Well at this point I believe i have addressed both issues that flareback pointed out. I have a second program that does almost the same exact thing and works the way it is supposed to. Only difference is that it flips the coin 1000 times as opposed to ten times and it doesnt have the print statements to display the result as either a head or a tail each time. Anyone have any idea why it works and the first program still returns 0 for heads and 100 for tails? the code for both programs is below. first is the program that doesnt work. Second is the program that does. public class FlipCoin { public static void main(String[] args) { int loopCount = 1; double result; double heads = 0.0; while(loopCount <= 10) { result = Math.random(); if(result <= .5){ System.out.println("The result is heads."); heads = heads++; } else System.out.println("The result is tails."); loopCount = loopCount + 1; } double percentHeads = heads/10*100; double percentTails = (10-heads)/10*100; System.out.println("the percentage of heads is: " +percentHeads); System.out.println("the percentage of tails is: " +percentTails); } } public class FlipCoin2 { public static void main(String[] args) { int loopCount = 1; double result; double heads = 0; while(loopCount <= 1000) { result = Math.random(); if(result <= .5) heads++; loopCount = loopCount + 1; } double percentHeads = heads/1000*100; double percentTails = (1000-heads)/1000*100; System.out.println("the percentage of heads is: " +percentHeads); System.out.println("the percentage of tails is: " +percentTails); } }
  3. rogue482

    Coin Flip Percentages

    Thanks to everyone who has contributed. I suck at this class. I think i understand what I need to do at this point the problem is that I seem to be unable to do it. as it stands now the code looks like this. public class FlipCoin { public static void main(String[] args) { int loopCount = 1; double result; int heads = 0; while(loopCount <= 10) { result = Math.random(); if(result <= .5) System.out.println("The result is heads."); if(result <= .5) heads = heads++; else System.out.println("The result is tails."); loopCount = loopCount + 1; } double percentHeads = heads/10*100; double percentTails = (10-heads)/10*100; System.out.println("the percentage of heads is: " +percentHeads); System.out.println("the percentage of tails is: " +percentTails); } } I'm sure this is still wrong, because it always returns percentages of 0 and 100 for heads and tails respectively. If anyone could point out my mistake it would be a huge help. Once again, thanks to any and all who have offered input.
  4. rogue482

    Coin Flip Percentages

    I apologize for the confusion. The assignment is actually in Java not C++. My apologies for not specifying earlier. Also, ClayMeow, i understand what you're saying about the math.random() function. I am simply following the directions I was given. airman, thank you for your input, however, the way you have your code set up to add to the heads variable exists the if function which leaves an else with no if. I'm not sure if this is because i failed to mention that the syntax was to be Java. Other than that the code works like a charm.
  5. rogue482

    Coin Flip Percentages

    The assignment was to write code to flip a coin 10 times and display the result as either heads or tails. Then we need to display the percentages of each result. Here is what i have so far. public class FlipCoin{ public static void main(String[] args) { int loopCount = 1; double result; while(loopCount < 10) { result = Math.random(); if(result <= .5) System.out.println("The result is heads."); else System.out.println("The result is tails."); loopCount = loopCount + 1; } } } My guess is that this is something simple and I'm going to feel stupid once someone shows me what I'm doing wrong. I know the above code flips the coin and displays the result as either a head or a tail, all i need is to know how to then get the percentages and display them.
×