Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating percent "x/y * 100" always results in 0?

Tags:

java

math

In my assignment i have to make a simple version of Craps, for some reason the percentage assignments always produce 0 even when both variables are non 0, here is the code.

import java.util.Random;

Header, note the variables

public class Craps {
private int die1, die2,myRoll ,myBet,point,myWins,myLosses;
private double winPercent,lossPercent;
private Random r = new Random();

Just rolls two dies and produces their some.

public int roll(){
    die1 = r.nextInt(6)+1;
    die2 = r.nextInt(6)+1;
    return(die1 + die2);
}

The Play method, this just loops through the game.

public void play(){
    myRoll = roll();
    point = 0;

    if(myRoll == 2 ||myRoll == 3 || myRoll == 12){
        System.out.println("You lose!");
        myLosses++;
    }else if(myRoll == 7 || myRoll == 11){
        System.out.println("You win!");
        myWins++;
    }else{
        point = myRoll;
        do {
            myRoll = roll();
        }while(myRoll != 7 && myRoll != point);
        if(myRoll == point){
            System.out.println("You win!");
            myWins++;
        }else{
            System.out.println("You lose!");
            myLosses++;
        }
    }
}

This is where the bug is, this is the tester method.

public void tester(int howMany){
    int i = 0;
    while(i < howMany){
        play();
        i++;
    }

bug is right here in these assignments statements

    winPercent = myWins/i * 100;
    lossPercent = myLosses/i* 100;
    System.out.println("program ran "+i+" times "+winPercent+"% wins "+ lossPercent+"% losses with "+myWins+" wins and "+myLosses+" losses");



}

}

like image 720
Patrick Beninga Avatar asked Nov 12 '12 04:11

Patrick Beninga


2 Answers

Probably because you are doing an integer division, and denominator is greater than numerator. So the value will be zero.

You can change this assignment: -

winPercent = myWins/i * 100;
lossPercent = myLosses/i* 100;

to: -

winPercent = myWins * 100.0/i;
lossPercent = myLosses * 100.0/i;

There are two things to consider: -

  • Either make your division a floating point of division, by casting your numerator to float: -

    winPercent = (float)(myWins)/i * 100;
    
  • Also, in case of integer division, its the matter of associativity and precendence. Since * and / have same precendence, and have left-to-right associativity. So, myWins / i will be evaluated first. So, there is bright chances that that value can be 0.

    So, just change the order of * and / to make the * happen first.

Note, I have used 100.0 in multiplication. Because multiplying by 100 will again result in Truncation.

like image 74
Rohit Jain Avatar answered Sep 30 '22 19:09

Rohit Jain


Take a look at

  winPercent = myWins/i * 100;
  lossPercent = myLosses/i* 100;

Since myWins, myLosses and i are all integers, Java is using integer arithmetic to return the result. Since myWins <= i, the result of myWins / i is always going to be 0 or 1.

You can fix this a number of ways, such as

   winPercent = (1.0f * myWins / i) * 100;
like image 25
sjr Avatar answered Sep 30 '22 20:09

sjr