简体   繁体   English

使用&& / ||和带有次要参数的用户输入进行while循环

[英]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. 我正在尝试输入一种条件,使用户陷入while循环中,直到他们输入介于.00.025之间的小数,且不得更高或更低。

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). 您在条件内两次调用keyboard.nextDouble() (尽管(第一个!)输入实际上为负,但只有第一个会被求值)。 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.) (版本0.25的条件在您的版本中也向后。)

You're calling nextDouble() twice, and your second condition is reversed. 您两次调用nextDouble() ,第二个条件相反。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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