I need to write a program that simulates 1000 coin tosses, then prints the resulting number of heads and tails. My main problem is with the "for" loop. This is what I have so far:
public static void main(String[] args) {
int tosses;
int headsTails = 0;
int tails = 0;
int heads = 0;
for (tosses = 0; tosses < 1000; tosses ++, headsTails = (int) (Math.random() * 2) + 1);
{
if (headsTails == 1) {
heads ++;
} else {
tails ++;
}
}
System.out.println("You flip a coin 1000 times.\nNumber of heads:" + heads + "\nNumber of tails:" + tails);
}
When I run this, I get either:
You flip a coin 1000 times.
Number of heads:0
Number of tails:1
or:
You flip a coin 1000 times.
Number of heads:1
Number of tails:0
So the program is only "tossing the coin" once. How would I go about making this program work as intended?
forloop. Remove the;at the end offordeclaration.