Originally Posted by
loui345
// The loop continues to execute as long as user is greater than or equal to 300
while(300<=user) {
System.out.println("confused");
user = sr.nextInt();
}
// The loop terminates when amount is less than 300
I don't understand your dilemma. The loop executes as long as the value of
user is greater than or equal to 300 but terminates when the value of
user is less than 300, right? What, exactly, did you expect?
Personally, my poor little peanut brain makes the connection better if I put the variable on the left of the comparison operator and the constant on the right because that's what I am used to, but the logic is exactly the same isn't it?
while (user >= 300) { // Same as (300 <= user)
// whatever
}
Maybe if you made your program a little more verbose???
import java.util.*;
public class Z {
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter an integer: ");
int amount = keyboard.nextInt();
System.out.println("Amount = " + amount);
// Loop will continue as long as amount is greater than or equal to 300
while (amount >= 300) { // Try it with (300 <= amount) also
System.out.print("\nEnter another integer: ");
amount = keyboard.nextInt();
System.out.println("Amount = " + amount);
}
System.out.println("Amount is less than 300. Goodbye for now.");
} // End main()
} // End class definition
Output:
Enter an integer: 301
Amount = 301
Enter another integer: 300
Amount = 300
Enter another integer: 299
Amount = 299
Amount is less than 300. Goodbye for now.
Cheers!
Z