【发布时间】: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