java - How to repeat code; If user inputs no. I want all three loops to repeat, until user inputs Yes -
// first loop number of quarters int quarter; while (true) { system.out.print("enter number of quarters (1-10): "); if (keyboard.hasnextint() && (quarter = keyboard.nextint()) >= 1 && quarter <= 10) break; keyboard.nextline(); // discard bad input system.out.println("number of quarters must between 1 , 10"); } keyboard.nextline(); // discard rest of line system.out.println("you have " + quarter + " quarters."); // second loop rate of intrest double intrestrate; while (true) { system.out.print("enter interest rate (5%-25%), without percent sign: "); if (keyboard.hasnextdouble() && (intrestrate = keyboard.nextdouble()) >= 5 && intrestrate <= 25) break; keyboard.nextline(); // discard bad input system.out.println("interest rate must between 5% , 25%"); } keyboard.nextline(); // discard rest of line system.out.println("you have selected " + intrestrate + "% rate of interest."); // third loop double balance; while (true) { system.out.print("enter beginning principal balance, greater zero: $"); if (keyboard.hasnextdouble() && (balance = keyboard.nextdouble()) > 0); break; }
okay assume, @ point, there branch asking if *input above correct, type y yes, or n no. if type y can code break. but, how make if user types in n (ignorecase) whole branch starts again? so, if user inputs no. program repeat, loop 1 through loop 3 until satisfied. (loops displayed in //code.
i'm not sure you're asking in terms of code, in general, way restart part of code if answer no put section of code in while loop.
//initialize string "no" while(user input "no"){ //your segment of code system.out.println("is correct?"); // take user input method of choice. yes break out of loop, no go start of while loop }
Comments
Post a Comment