5

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



}

}

  • 2
    You're doing integer arithmetic. You're getting exactly the answer you asked for. – Hot Licks Nov 12 '12 at 04:53
  • Many duplicates: http://stackoverflow.com/questions/10455677/division-in-java , http://stackoverflow.com/questions/7220681/division-of-integers-in-java , http://stackoverflow.com/questions/13148977/strange-zero-when-divide –  Nov 12 '12 at 05:17

4 Answers4

16

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.

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
  • No. It's not just a matter of associativity and precedence. `(float)myWins/i * 100` would have [for all intents and purposes] the same result -- and it *parses the same way*. Furthermore, the latter example if changed to `myWins * 100/i` is *still wrong*, just differently. –  Nov 12 '12 at 05:06
  • @pst.. Ah! That would also work. I just considered here integer division. Would add that. – Rohit Jain Nov 12 '12 at 05:07
3

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;
sjr
  • 9,258
  • 1
  • 22
  • 35
0

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;

Yuca
  • 5,048
  • 2
  • 17
  • 38
0

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

user3701435
  • 107
  • 7