After creating a simple java craps game, I decided to have it test for 1 million iterations. For whatever reason, dividing the two numbers with this code
System.out.println("Wins: "+w+" Losses: "+l);
double avg = 100*(w / l);
System.out.println("Average: "+avg);
This for whatever reason prints the correct number of wins and losses but when it divides it just gets 0.0 Any help would be much appreciated.
Resolved code:
double w = 0 , l = 0;
for(int c=1;c<1000000;c++)
{
if(playGame() == true)
{
System.out.println("You win!");
w++;
}
else
{
System.out.println("You lost!");
l++;
}
}
System.out.println("Wins: "+w+" Losses: "+l);
double avg = (100*(w / l))/2;
System.out.println("Average: "+avg);
i suspect that your
wandlmight be ints. when you divide ints, they don’t evaluate to doubles, they evaluate to ints, and they always round downNow you have 2 options if that indeed is the case
wandlas doubles during inputwandlto doubles before you don any arithmetic on them.try: