I'm currently doing an assignment for school, and I need to create a program that uses Math.random() to get a random value, and depending on the value, output "Heads" or "Tails". It needs to do this 10 times. Then, it needs to find the percentage of times that the program output heads, and how many it output tails. However, it isn't working correctly. It always outputs that heads/tails were 0% of the tosses. Can someone please explain why?
public class HeadsTails
{
public static void main(String[] args)
{
int v;
double i;
int heads = 0, tails = 0;
double headsPercent, tailsPercent;
for(v = 1; v <= 10; ++v)
{
i = Math.random();
if(i <= 0.5)
{
System.out.println("Heads");
heads = heads + 1;
}
else if(i > 0.5)
{
System.out.println("Tails");
tails = tails + 1;
}
}
headsPercent = (heads / 10) * 100;
tailsPercent = (tails / 10) * 100;
System.out.println("Heads were " + headsPercent + "% of the tosses.");
System.out.println("Tails were " + tailsPercent + "% of the tosses.");
}
}
I am also quite open to any improvements that can be made, besides those that just make the program function properly.