如何使用scanner只接受int或阻止用户输入double?

时间:2016-01-18 08:34:16

标签: java java.util.scanner decimalformat

这是我目前的代码。我想做的是将用户输入限制为仅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));

    }

}

2 个答案:

答案 0 :(得分:2)

如下所示创建用户定义的方法 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 (!scanner.hasNextInt()) {
    scanner.next();
    System.out.println("Please enter a whole number");//message
}

答案 1 :(得分:0)

1.use try {} catch {} block(引用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));
}

}