-9

Does anyone know why the first condition is being skipped by Java?

while((withdraw % 100) != 0 && (withdraw > startBalance))

Although I state that the withdrawal must be less than the startBalance, you can still type a number that is higher than the startBalance and the the newBalance will be negative.

Here's my code:

public static void main(String[] args) {

  Scanner input = new Scanner(System.in);
  int startBalance = 1000;
  System.out.println("Please enter how much you want to withdraw: ");
  int withdraw = input.nextInt();

  while((withdraw % 100) != 0 && (withdraw > startBalance)){
    System.out.println("Sorry, you can only withdraw a value multiple of 100 (we only have 100 SEK bills): ");
    withdraw = input.nextInt();
  }

  int newBalance = startBalance-withdraw;
  System.out.println("Thanks! Your new balance is: SEK " + newBalance);
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710

4 Answers4

4

If the first condition is false then it wont consider the second condition. If first condition is true then it will also evaluate the second condition. This is because you are using && operation. If you use || then if the first one is false it will evaluate the next condition.

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
3
while((withdraw % 100) != 0 && (withdraw > startBalance))

Let me read out your condition in plain words:

"Keep looping as long as both of these conditions hold:

  • the requested amount is not round;
  • it is larger than start balance."

So, let's say we request 1,000,000. Is it "not round"? No. Do both conditions hold? No. Therefore, the loop is over.

As a side point, this has nothing to do with the distinction between & and && or the order of evaluation. It is just plain, common-sense logic.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

try this code:

import java.util.Scanner;

public class Balance {

    public static void main(String[] args) {

          Scanner input = new Scanner(System.in);
          int startBalance = 1000;
          System.out.println("Please enter how much you want to withdraw: ");
          int withdraw = input.nextInt();

          while((withdraw % 100) != 0 || (withdraw > startBalance)){
            System.out.println("Sorry, you can only withdraw a value multiple of 100 (we only have 100 SEK bills): ");
            withdraw = input.nextInt();
          }

          int newBalance = startBalance-withdraw;
          System.out.println("Thanks! Your new balance is: SEK " + newBalance);
        }
}

I used

|| (or)

instead of

&& (and)

because we ALWAYS need to check if the user has enough balance :)

triForce420
  • 719
  • 12
  • 31
0

My assumption is you don't want the loop to check if the modulo % of 100 does not equal AND the withdrawal is greater than the start balance.

Double ampersands && check if both the arguments are true and unless your starting balance was less than 0, the conditions for the while loop will never be met.

Therefore, you want to use the or operators || which will check if either one or the other conditions are met which is what you are looking for.

Change: while((withdraw % 100) != 0 && (withdraw > startBalance))

To: while((withdraw % 100) != 0 || (withdraw > startBalance))

This will fix your issue.

Darrell
  • 638
  • 4
  • 17