-2

i am trying to run this code in java

double health = ((level*level +10000) * (level/150)/6) +100;

and it always come out as 100 (or whatever the last number is) in this instance level = 1 but even replacing the variable level with the number 1 wont fix this. when done right the answer should equal approximately 110 . please help me fix this code so i can continue programming

i have already tried breaking the line up into multiple separate lines of code, but still no fix. granted, this uses some pretty big fractions but it worked before and not all of a sudden it has stopped working. please help me to fix this problem,

thanks in advance.

arcturus125
  • 11
  • 1
  • 2

3 Answers3

2

((level*level +10000) * (level/150)/6) get's floored off to the nearest number, which is 0.

So your code should be like this:

public static void main(String[] args) {

    double level = 10.0;

    double health = ((level*level +10000) * (level/150)/6) +100;

    System.out.println(health);
 }
} 
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
1

Assuming level is an int, level/150 will automatically get floored to a whole number, or 0. You can solve this by changing level to a float, or by changing 150 to 150f.

shmosel
  • 49,289
  • 6
  • 73
  • 138
0

fixed it. i needed to put (double) infront of the fraction

double health = ((1*1 +10000) * ((double)1/150)/6) +100;

without it the number is too small and the program must assumes it is 0

arcturus125
  • 11
  • 1
  • 2