【问题标题】:How to use scanner to accept only int or prevent users from inputting double? [duplicate]如何使用扫描仪只接受 int 或阻止用户输入 double? [复制]
【发布时间】:2016-01-18 08:34:15
【问题描述】:

这是我当前的代码。我想做的是将用户输入限制为仅 int。如果输入双精度数,我想要一行打印“请输入一个整数”。我尝试过使用scanner.hasNextint,但没有取得多大成功。有没有办法完全忽略 double 作为输入并让它循环?

提前致谢!

public class BMI extends DecimalFormat{

    public static void main(String[] args) {
        int weight;
        int height;
        double bodyMassIndex;

        DecimalFormat dfWithTwoDecimalPlaces;
        Scanner scanner;
        scanner = new Scanner(System.in);
        System.out.print("What is your weight in kilograms (kg): ");
        weight = scanner.nextInt();
        System.out.print("What is your height in centimeters(cm): " );
        height = scanner.nextInt();
        bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );


        dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
        System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));

    }

}

【问题讨论】:

    标签: java java.util.scanner decimalformat


    【解决方案1】:

    创建一个用户定义的方法getNextInt()如下:

    /**get next integer*/
    public static int getNextInt(Scanner scanner) {
        while (!scanner.hasNextInt()) {
            scanner.next();
        }
        return  scanner.nextInt();
    }    
    
    public static void main(String[] args) {
        int weight;
        int height;
        double bodyMassIndex;
    
        DecimalFormat dfWithTwoDecimalPlaces;
        Scanner scanner;
        scanner = new Scanner(System.in);
        System.out.print("What is your weight in kilograms (kg): ");
        weight = getNextInt(scanner);//call user-defined method
        System.out.print("What is your height in centimeters(cm): " );
        height = getNextInt(scanner);//call user-defined method
        bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );
    
    
        dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
        System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
    
    }
    

    您可以像这样在 while 循环中添加一条消息:

    while (!scanner.hasNextInt()) {
        scanner.next();
        System.out.println("Please enter a whole number");//message
    }
    

    【讨论】:

    • 这对我不起作用,但我没有时间完全尝试解决返回的错误。我没有要求这个任务,但是我不得不把它上交,所以我按原样上交,并将在我的空闲时间继续工作。我感谢大家的回应!谢谢!如果我卡住并发布我遇到的错误,我会回复。
    【解决方案2】:

    1.使用try {} catch{}块(参考java异常处理)

    public class MySource extends DecimalFormat{
    public static void main(String[] args) {
         int weight;
            int height;
            double bodyMassIndex;
    
            DecimalFormat dfWithTwoDecimalPlaces;
            Scanner scanner;
            scanner = new Scanner(System.in);
    
            while(true) {
                try {
                    System.out.print("What is your weight in kilograms (kg): ");
                    weight = scanner.nextInt();
                    System.out.print("What is your height in centimeters(cm): " );
                    height = scanner.nextInt();
                    break;
                }catch(InputMismatchException e) {
                    System.out.println("Please enter a whole number.");
                    scanner.nextLine();
                }
            }
    
    
            bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );
    
    
            dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
            System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2022-10-02
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多