如何为用户输入创建循环(直到用户输入有效输入)? [英] How can I create a loop for user input (until the user enters valid input)?

查看:86
本文介绍了如何为用户输入创建循环(直到用户输入有效输入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My program should ask user to enter the amount they want to withdraw from their account and calculating the current balance after withdrawing. The requirement for withdrawing is minimum 100 and maximum 1000. If the user enters wrong input the program should re-promt and ask user to enter amount again. This process will keep repeating until the user puts right amount. Once the right amount is choosen it should calculate and display the current balance.

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

This is how I tried, but I failed to make the loop:

    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);
           }
       }
    }


推荐答案

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

Use while loop with TRUE condition and break when you want to break.

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;
               }
           }
           }
        }

这篇关于如何为用户输入创建循环(直到用户输入有效输入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆