0

My code:

public class Test {

public static int l = 29;
public static int w = 16;
public static int total = w + l;
public static int result = w / total;
public static int Result = total * 100;

 public static void main(String[] args) {

        System.out.println("You're W/L ratio is: " + (Result) + "%"); // Display the string.
 }
}

Response in console: You're W/L ratio is: 0%

Raul Rene
  • 10,014
  • 9
  • 53
  • 75
Etienne
  • 23
  • 2

3 Answers3

2

You're doing int division which always returns an int and so w / total will always be 0 since total is always greater than w. Do the multiplication by 100 first.

int result = (w * 100) / total;

Also you will want to learn and use java naming rules. Variable names should begin with a lower case letter.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Do the calculation by casting w and total to double, or just make them double to begin with. For the first option:

public static double result = (double)w / (double)total;
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
1

whenever you do division when both numbers are integers, in result you will get integer in return.

which actually means you will get floor value of (a/b); ie:

1/2=0 as 1/2=> 0.5> floor of 0.5= 0;

3/4=0 as 3/4=> 0.75> floor of 0.75= 0;

etc

Community
  • 1
  • 1
user902383
  • 8,420
  • 8
  • 43
  • 63