如何为用户输入创建循环(直到用户输入有效输入)?

我的程序应该要求用户输入他们想要从帐户中提取的金额,并在提款后计算当前余额。 提款要求最低为100,最高为1000.如果用户输入错误输入,程序应重新提交,并要求用户再次输入金额。 此过程将一直重复,直到用户输入正确的金额。 选择正确的金额后,应计算并显示当前余额。

这是我尝试的方式,但我没有做出循环:

package ex3; import java.util.Scanner; public class BankApp { public static void main(String[] args) { //displaying the welcome message System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n"); //initializing all necessary variables double initialBalance = 1000; double userChoise = 0; double currentBalance; //asking user to enter expected amount System.out.println("Enter your amount you want to withdraw (in SEK): "); //creating new instance of the scanner class Scanner iScanner = new Scanner(System.in); //store into userChoise whatever amount is chosen by user userChoise = iScanner.nextDouble(); //checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000. if(userChoise  1000) { System.out.println("Error: Enter your amount again(in SEK): "); } else { currentBalance = initialBalance - userChoise; System.out.printf("You have withdrawn %.2f\n", userChoise); System.out.printf("Your current balance is %.2f\n", currentBalance); } } } 

使用带有TRUE条件的while循环,并在想要中断时中断。

 package ex3; import java.util.Scanner; public class BankApp { public static void main(String[] args) { //displaying the welcome message System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n"); //initializing all necessary variables double initialBalance = 1000; double userChoise = 0; double currentBalance; //asking user to enter expected amount System.out.println("Enter your amount you want to withdraw (in SEK): "); //creating new instance of the scanner class Scanner iScanner = new Scanner(System.in); while(true){ //store into userChoise whatever amount is chosen by user userChoise = iScanner.nextDouble(); //checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000. if(userChoise < 100 || userChoise > 1000) { System.out.println("Error: Enter your amount again(in SEK): "); } else { currentBalance = initialBalance - userChoise; System.out.printf("You have withdrawn %.2f\n", userChoise); System.out.printf("Your current balance is %.2f\n", currentBalance); break; } } } }