简体   繁体   English

java - 如何限制用户在一定范围内输入数字?

[英]How to restrict the user to enter number between certain range in java?

In Java, if you want to ask users, for example, to input numbers only between 1,000 and 100,000, the program would continue further.例如,在 Java 中,如果您希望用户只输入 1,000 到 100,000 之间的数字,则程序将继续进行。 Otherwise, ask the user to input the number again in the given range.否则,要求用户在给定范围内再次输入数字。

Edit: Here, my program only tries to validate the input only once.编辑:在这里,我的程序只尝试验证输入一次。 How shall I make to ask the user to input until valid data(ie between 1000-100000) is entered我该如何要求用户输入直到输入有效数据(即在 1000-100000 之间)

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);    
    System.out.print("Principal:");
    double principal = sc.nextDouble();

    if(principal<1000 || principal >100000) {
        System.out.println("enter principal again:");
        principal = sc.nextDouble();        
    }
}

You can use do-while loop here as below您可以在此处使用 do-while 循环,如下所示

public static void main(String []args){
    Scanner in = new Scanner(System.in);
    int result;
   do {
       System.out.println("Enter value bw 1-100");
       result = in.nextInt();
   } while(result < 0 || result > 100);

    System.out.println("correct "+ result);

}
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;

class Myclass {

    static boolean isInteger(double number){
        return Math.ceil(number) == Math.floor(number); 
    }
    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);    
        double num;
        do{
            System.out.print("Enter a integer between 1000 and 100000 : ");
            num = in.nextDouble();
        }
        while((num < 1000 || num > 100000)||(!isInteger(num)));

        System.out.println("Validated input  : "+num);

    }
}

Hope this helps you希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中,如何限制用户只能输入一种类型? - In Java, how to restrict a user to enter only one type? Java-如何使用文档过滤器限制输入数字范围? - Java-How to restrict input number range with is Document Filter? 如何让用户使用正则表达式 java 输入某种格式 - How to make user enter a certain format using regex java 如何在Java中将用户输入限制为整数1或2,并且不允许用户输入除整数以外的任何内容? - How to restrict user input to the integer 1 or 2 and not allow user to enter anything other than a integer in java? Java用户输入-输入一定范围内的整数或循环返回,然后重试 - Java User Input - Enter integer within certain range or loop back and try again 如何限制用户可以输入的字符数? - How to restrict amount of characters a user can enter? 如何限制用户在Java Swing的JTextField中输入数字或特殊字符,空格或空格? - How to restrict user to enter digits or special charater or blank or space in JTextField in Java Swing? Java - 如何正确验证特定范围内的数字用户输入 - Java - How to Validate Numerical User Input Within A Certain Range Correctly 将Java程序之间的通信限制为同一用户 - Restrict communication between Java programs to same user 在Java中生成特定范围的随机双数 - Generating a random double number of a certain range in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM