Patrick Beninga
Patrick Beninga

Reputation: 53

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

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");



}

}

Upvotes: 5

Views: 5280

Answers (4)

Ankush Wasankar
Ankush Wasankar

Reputation: 1

Problem Statements:

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

Given:
myWins or myLosses = integer
i = interger

Reason:
Division maybe coming in decimal, which is stored internally by compiler in integer format. As value passed is in integer format so then only integer part (Before decimal part) is stored in temp location. So actual value coming zero.

Solution:
winPercent = ((float)myWins/i)*100;

Upvotes: 0

user3701435
user3701435

Reputation: 107

Do not typeconvert to int. Use float or double. As you are doing integer arithmetic, the decimal parts are not being considered.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213261

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.

Upvotes: 16

sjr
sjr

Reputation: 9885

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;

Upvotes: 3

Related Questions