Java 如何将用户输入限制为数值?

Java 如何将用户输入限制为数值?,java,input,java.util.scanner,Java,Input,Java.util.scanner,我一直在网上搜索,似乎找不到这个答案 那么,有人知道如何阻止用户在只允许数字的地方输入字母吗 到目前为止,我的代码就是这样的 public static double payCalculator(double hours, double basePay) { double totalPay; double overTime = 8.00 * 1.5; while(hours < 0 | hours > 60) { System.out.pri

我一直在网上搜索,似乎找不到这个答案 那么,有人知道如何阻止用户在只允许数字的地方输入字母吗

到目前为止,我的代码就是这样的

public static double payCalculator(double hours, double basePay)
{
    double totalPay; 
    double overTime = 8.00 * 1.5;
    while(hours < 0 | hours > 60) {
        System.out.println("Cannot work more than 60 hours a week");
        System.out.println("Hours Work?");
        hours = in.nextDouble();
    }
    while(basePay < 8) {
        System.out.println("Base Pay cannot be less than 8");
        System.out.println("Base Pay?");
        basePay = in.nextDouble();
    }
    if 
        (hours <= 40){
        totalPay = hours*basePay;
    }
    else {
        totalPay = ((hours - 40)*overTime) + (40*basePay);
    }
    return totalPay;

}
public static void main (String[] args) {

    System.out.println("Hours worked?");
    hours = in.nextDouble();
    System.out.println("Base Pay?");
    basePay = in.nextDouble();
    DecimalFormat df = new DecimalFormat("###.##");
    totalPay = payCalculator(hours, basePay);
    System.out.println("Total Pay is " + df.format(totalPay));       
}
}
公共静态双薪计算器(双倍小时,双倍底薪)
{
双倍全薪;
双倍加班=8.00*1.5;
而(小时数<0 |小时数>60){
System.out.println(“每周工作时间不能超过60小时”);
System.out.println(“工时?”);
小时=in.nextDouble();
}
而(基本工资<8){
System.out.println(“基本工资不能低于8”);
System.out.println(“基本工资?”);
basePay=in.nextDouble();
}
如果

(小时我假设您正在使用
扫描仪
进行输入

您可以使用
Scanner.hasNextDouble()
验证它是否为数字,如果此扫描仪输入中的下一个标记可以使用
nextDouble()
方法解释为
double
值,则返回true。扫描仪不会超过任何输入

考虑这个例子,它会一直要求输入,除非用户提供一个数字

    Scanner sc = new Scanner(System.in);
    double dbl = 0.0;
    boolean isValid = false;
    while (isValid == false) {
        System.out.println("Input Number: ");
        // If input is number execute this,
        if (sc.hasNextDouble()) {
            dbl = sc.nextDouble();
            isValid = true;
            System.out.println("OK");
        }
        // If input is not a number execute this block, 
        else {
            System.out.println("Error! Invalid number. Try again.");
        }
        sc.nextLine(); // discard any other data
    }
    sc.close();
输出

Input Number: 
adsads
Error! Invalid number. Try again.
Input Number: 
sdas
Error! Invalid number. Try again.
Input Number: 
hello
Error! Invalid number. Try again.
Input Number: 
hi
Error! Invalid number. Try again.
Input Number: 
2.0
OK

我假设您正在使用
扫描仪
获取输入

您可以使用
Scanner.hasNextDouble()
验证它是否为数字,如果此扫描仪输入中的下一个标记可以使用
nextDouble()
方法解释为
double
值,则返回true。扫描仪不会超过任何输入

考虑这个例子,它会一直要求输入,除非用户提供一个数字

    Scanner sc = new Scanner(System.in);
    double dbl = 0.0;
    boolean isValid = false;
    while (isValid == false) {
        System.out.println("Input Number: ");
        // If input is number execute this,
        if (sc.hasNextDouble()) {
            dbl = sc.nextDouble();
            isValid = true;
            System.out.println("OK");
        }
        // If input is not a number execute this block, 
        else {
            System.out.println("Error! Invalid number. Try again.");
        }
        sc.nextLine(); // discard any other data
    }
    sc.close();
输出

Input Number: 
adsads
Error! Invalid number. Try again.
Input Number: 
sdas
Error! Invalid number. Try again.
Input Number: 
hello
Error! Invalid number. Try again.
Input Number: 
hi
Error! Invalid number. Try again.
Input Number: 
2.0
OK

您不需要发布全部代码来找到问题的解决方案,请只共享与问题相关的部分代码。似乎是一个控制台应用程序。是否可以使用带有Swing输入文本字段的GUI。Sufiyan Ghori-所有代码都与我的问题相关,我不确定答案会在哪里。@Korwynwagstaff,不是所有代码都是相关的,例如,
main
方法中的代码足以理解您的要求,因此不需要包含
payCalculator()
method与您的代码一起使用。您不需要发布全部代码来找到问题的解决方案,请只共享与您的问题相关的部分代码。似乎是一个控制台应用程序。是否可以使用带有Swing输入文本字段的GUI。Sufiyan Ghori-所有代码都与我的问题相关,我不确定答案在哪里ould go.@Korwynwagstaff,并非所有代码都是相关的,例如,
main
方法中的代码足以理解您的要求,因此不需要在代码中包含
payCalculator()
方法。