有誰知道爲什麼第一個條件被Java跳過?爲什麼Java在while循環中跳過我的第二個條件?
while((withdraw % 100) != 0 && (withdraw > startBalance))
雖然我的狀態,撤離必須小於startBalance
,你仍然可以鍵入一個數字,比startBalance
和該newBalance
將是負面的更高。
這裏是我的代碼:
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);
}
使用&代替&&如果您想要評估第二個條件 – borchero