简体   繁体   English

while循环中的用户输入

[英]user input within while loop

I was asked to run a loop that asks for user input, applies the change using the adjustPrice() method, print the new information after adjusting the price. 我被要求运行一个循环,询问用户输入,使用adjustPrice()方法应用更改,调整价格后打印新信息。 and then finishes the loop when the user enters 0. 然后在用户输入0时完成循环。

Right now it does all of the above, just doesn't ask for the user input again and ends with the printed new information. 现在它完成了上述所有操作,只是不再询问用户输入并以打印的新信息结束。 please help! 请帮忙!

    boolean done = false;
    while (!done) {
       System.out.print("Enter adjustment to price in percent (0 to quit): ");
       double adjustment = in.nextDouble();

       if (adjustment == 0) {
           done = true; 
       }else{
           swag.adjustPrice(adjustment);
           System.out.println(swag.toString());
           in.next();
       }    
   }

You have in.next() at the end. 你最后有in.next()。 This expects the user to input something before the loop will reset to the System.out line. 这要求用户在循环重置为System.out行之前输入内容。 Take out that line. 取出那条线。

to allow user input using the end 允许用户输入结束

else {
   swag.adjustPrice(adjustment);
   System.out.println(swag.toString());
   in.next();
} 

but beware that this method does not take keys as enter or space to take into account these keys use. 但请注意,此方法不会将键作为输入或空格来考虑这些键使用。

else {
   swag.adjustPrice(adjustment);
   System.out.println(swag.toString());
   System.in.read();
}

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

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