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

时间:2014-11-24 17:32:03

标签: java loops if-statement while-loop java.util.scanner

我的程序应该要求用户输入他们想要从帐户中提取的金额,并在提款后计算当前余额。 提款要求最低为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 < 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);
           }
       }
    }

1 个答案:

答案 0 :(得分:1)

使用带有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;
               }
           }
           }
        }