如何在 Java 中限制用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33050973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:07:55  来源:igfitidea点击:

How To Limit User Input In Java

javainput

提问by john smith

The amount of investment must be positive and can be any value. The period of investment is in years so should be positive. The annual rate of interest can be between 0.25% to 14%.

投资金额必须是正数,可以是任何值。投资期限以年为单位,所以应该是正数。年利率可介于 0.25% 至 14% 之间。

import java.util.Scanner;

public class InterestCalculator{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Scanner input = new Scanner(System.in);

        // Entering the interest rate
        System.out.print("Please Enter the annual interest rate between 0.25 to 10 : ");
        double annualInterestRate = input.nextDouble();

        double monthlyInterestRate = annualInterestRate / 1200;

        System.out.print("Enter number of years: ");
        int numberOfYears = input.nextInt();

        // Entering the amount earned
        System.out.print("Enter Amount: ");
        double Amountofinterest = input.nextDouble();

        // Calculating
        double moneyearned = Amountofinterest * monthlyInterestRate;

        // Displaying the results
        System.out.println("The money earned is $" +
        (int) (moneyearned * 100) / 100.0);
        int i;

        for (i = 1; i <= numberOfYears * 12; i++) {
            double Balance = Amountofinterest + moneyearned;
            Amountofinterest = Balance;
            monthlyInterestRate = moneyearned + 0.01;
            System.out.println(i + "\t\t" + Amountofinterest
                    + "\t\t" + monthlyInterestRate + "\t\t" + Balance);

        }

    }
}

I have made the basic program but, I don't know how to add restrictions.

我已经制作了基本程序,但我不知道如何添加限制。

回答by Arc676

You can use loops that repeatedly ask for input until it's valid:

您可以使用重复要求输入直到输入有效的循环:

double annualInterestRate = 0;
while (annualInterestRate < 0.25 || annualInterestRate > 10){
    System.out.print("Please Enter the annual interest rate between 0.25 to 10 : ");
    annualInterestRate = input.nextDouble();
    if (annualInterestRate < 0.25 || annualInterestRate > 10){
        System.out.println("Please enter a value between 0.25 and 10");
    }
}
//if you reach this point, input is valid because it is neither <0.25 or >10

You can do this for all values that need to meet certain criteria. Just make sure you initialize the variable beforethe loop and set it as an invalid value, otherwise the loop won't run.

您可以对需要满足特定条件的所有值执行此操作。只要确保在循环之前初始化变量并将其设置为无效值,否则循环将不会运行。

Other variables:

其他变量:

int numberOfYears = -1; //or 0 is you don't allow 0 years
while (numberOfYears < 0){ //or <= 0 if you don't allow 0 years
    System.out.print("Enter number of years: ");
    numberOfYears = input.nextInt();
}
double Amountofinterest = -1; //or 0
while (Amountofinterest < 0){ //or <= 0
    System.out.print("Enter Amount: ");
    Amountofinterest = input.nextDouble();
}

回答by Rehman

Are you talking about this :

你在说这个吗:

while(input.nextDouble()<0){
         System.out.println("Please enter positive investment");

     }

回答by Aven

Ok you could then use a while loop like this:

好的,你可以像这样使用 while 循环:

    int numberOfYears = -1;
    System.out.print( "Enter number of years: ");
    while(numberOfYears < 0){ 
    numberOfYears = input.nextInt();
    }