I currently have this code:
int kills = 1;
int deaths = 2;
double kdr = 0;
if(kills > 0 && deaths == 0) {
kdr = kills;
} else if(kills > 0 && deaths > 0){
kdr = kills/deaths;
}
System.out.println(kdr);
You can test it here.
Why is the output 0.00 and not 0.5?
If
kills/deaths< 1, you get0, since the output of integer division is integer. This0is then cast to0.0to fit thedoublevariable in which you store it.In order to get a non-integer result, you have to cast one of the numbers to double :