简体   繁体   中英

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. and then finishes the loop when the user enters 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. This expects the user to input something before the loop will reset to the System.out line. 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();
}

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