如何在Java中限制用户输入

如何在Java中限制用户输入,java,input,Java,Input,投资金额必须为正,可以是任何价值。 投资期限为年,因此应为正值。 年利率可以在0.25%到14%之间 import java.util.Scanner; public class InterestCalculator{ /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code applicatio

投资金额必须为正,可以是任何价值。 投资期限为年,因此应为正值。 年利率可以在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);

        }

    }
}
import java.util.Scanner;
公共类利益计算器{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪输入=新扫描仪(System.in);
//输入利率
系统输出打印(“请输入0.25到10之间的年利率:”;
double annualInterestate=input.nextDouble();
双月利率=年利率/1200;
System.out.print(“输入年份:”);
int numberOfYears=input.nextInt();
//输入赚取的金额
系统输出打印(“输入金额:”);
double AmountFinTerest=input.nextDouble();
//算计
双倍收入=每月利息*每月利息;
//显示结果
System.out.println(“赚的钱是美元”+
(int)(赚得的钱*100)/100.0);
int i;

对于(i=1;i,可以使用循环重复请求输入,直到输入有效:

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
双年度利率=0;
而(年利率<0.25 | |年利率>10){
系统输出打印(“请输入0.25到10之间的年利率:”;
annualInterestate=input.nextDouble();
如果(年利率<0.25 | |年利率>10){
System.out.println(“请输入一个介于0.25和10之间的值”);
}
}
//如果您达到这一点,输入是有效的,因为它不是10
您可以对需要满足特定条件的所有值执行此操作。只需确保在循环之前初始化变量并将其设置为无效值,否则循环将无法运行

其他变量:

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();
}
int numberOfYears=-1;//或者0表示不允许0年

当(numberOfYears<0){//或你在谈论这个问题时:

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

     }

while(input.nextDouble()Ok然后可以使用如下while循环:

    int numberOfYears = -1;
    System.out.print( "Enter number of years: ");
    while(numberOfYears < 0){ 
    numberOfYears = input.nextInt();
    }
int numberOfYears=-1;
System.out.print(“输入年份:”);
而(numberOfYears<0){
numberOfYears=input.nextInt();
}

Ok.
if
语句怎么样?另请参见:您正在定义一个新的
double annualinterestate
。您应该删除
double
。好眼力。我的坏。编辑了它谢谢..这对年率限制非常有效…您能告诉我另外两个限制吗…非常感谢您的支持e帮助和快速回复。