简体   繁体   中英

While loop with user input with minor parameters using && / ||

I'm trying to enter a condition where the user gets caught in a while loop until they enter a decimal between .00 and .025 , no higher or lower.

System.out.println("Enter the interest rate you would like, preferablly between .00 and .25");
while ((interestRate = keyboard.nextDouble()) < 0.00 || ((interestRate = keyboard.nextDouble()) < 0.25))
{
    System.out.println("Just a number between .00 and .25, no more, no less!");
}
System.out.println("Testing for break");

I'm unable to get the accursed thing to run both ways properly, but always run into a blank.

You're calling keyboard.nextDouble() twice inside the condition (although only the first will be evaluated if the (first!) input is in fact negative). Probably, don't call it at all inside the condition:

while(true) {
  interestRate=keyboard.nextDouble();
  if(interestRate>=0 && interestRate<=0.25) break;
  System.out.println("...");
}

(The conditional for 0.25 was also backwards in your version.)

You're calling nextDouble() twice, and your second condition is reversed. Try this:

while ((interestRate = keyboard.nextDouble()) < 0.00 || interestRate > 0.25)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM