0

The variable gallonsLeft are not actually being used, it's just to show why one set up is working and the other doesn't. See comment in code. On a calculator I get the same outputs, not sure why I don't in the program. Maybe something to do with data type used? idk...

    System.out.println("Tank Capacity: ");
    int tankTotal = input.nextInt();
    System.out.println("Gauge reading: ");
    int userGauge = input.nextInt();
    System.out.println("Miles per gallon: ");
    int mpg = input.nextInt();

    int gallonsLeft = ((userGauge * tankTotal)/100); // This prints out the correct number 
    int gallonsLeft = ((userGauge/100)* tankTotal); // This prints out 0
    System.out.println(gallonsLeft);
VKFLOW
  • 1
  • 1

1 Answers1

0

The problem because (userGauge/100) result is not int. The int part is zero so you need to choose another datatype for this :

double gallonsLeft = ((userGauge/100)* tankTotal); // This prints out now the correct number
JavaMind
  • 11
  • 1
  • 6
  • Thank you I appreciate it. To confirm, if I'm using an int variable, all parts of my arithmetic has to always remain as integers? I figured I could use double but the practice problem I'm doing requires me to use only integers. – VKFLOW Aug 07 '20 at 18:31