好吧,我刚刚开始学习编码,但很快就陷入困境。我有很多问题,而且我是新手,所以我将多次发布我的代码并提出不同的问题。
我的问题:我希望用户在 System.out.println("Define your height in cmimeters.");
和 之后输入一个
.我希望我的代码对其进行限制,以便只能输入 double 值,而不能以类似的方式输入任何其他内容,用户不能在程序的第一部分中输入除男性/女性(和派生)之外的任何内容。根据我到目前为止所学到的知识,我可以捕获一个异常。首先,每次尝试时它并不适合我,其次,我希望我的程序能够以一种不会发生异常的方式工作。那可能吗?我实在找不到解决办法。 double
System.out.println("定义你的体重,以千克为单位。");
我从尝试做一个简单的体重指数开始学习。让我们忽略男性和女性值(value)观相同的事实,我将来会改变这一点。 这是我的代码:
import java.util.*;
class BMI_a {
public static void main(String[] args) {
String answer;
do {
double weight, height;
String sex;
String Stare = System.getProperty("user.name");
System.out.println("... BMI meter v.0.0.25 ...");
System.out.println("Welcome to the program " + Stare + "!");
boolean sexgood = false;
Scanner sexscanner = new Scanner(System.in);
System.out.println("Define your gender. (Male/Female)");
do {
sex = sexscanner.next();
if(sex.equals("Male") || sex.equals("male") || sex.equals("M") || sex.equals("m")) {
sexgood = true;
}
else if(sex.equals("Female") || sex.equals("female") || sex.equals("F") || sex.equals("f")) {
sexgood = true;
}
else {
System.out.println("Did you get it right? Choose either 'Male' or 'Female'. You can also use M/F.");
}
}
while (!sexgood);
Scanner heightscanner = new Scanner(System.in);
System.out.println("Define your height in centimetres.");
height = heightscanner.nextDouble();
Scanner weightscanner = new Scanner(System.in);
System.out.println("Define your weight in kilograms.");
weight = weightscanner.nextDouble();
double readier = weight / ((height * height) / (100 * 100));
System.out.println("Your BMI is: " + readier);
System.out.println("Normal BMI is between 18.5 and 24.9");
if (sex.equals("Male") || sex.equals("male") || sex.equals("M") || sex.equals("m")) {
if (readier <= 18.5){
System.out.println("You're probably gonna die of malnutrition soon. Go grab a snack.");
}
else if (readier <= 24.9) {
System.out.println("You're fit and ready to get fat.");
}
else if (readier <= 29.9) {
System.out.println("You're already quite fat. put that chocolate back!");
}
else {
System.out.println("Oh boy. You're fat. Really fat.");
System.out.println("You can still be a famous sumo wrestler though!");
}
}
else {
if (readier <= 18.5){
System.out.println("You're probably gonna die of malnutrition soon. Go grab a snack.");
}
else if (readier <= 24.9) {
System.out.println("You're fit and ready to get fat.");
}
else if (readier <= 29.9) {
System.out.println("You're already quite fat. put that chocolate back!");
}
else {
System.out.println("Oh boy. You're fat. Really fat.");
System.out.println("You can still be a famous sumo wrestler though!");
}
}
System.out.println("If you'd like to run the program again, press 'Y' and confirm.");
System.out.println(" If you'd like to exit now, press any other key and confirm. ");
Scanner answerscanner = new Scanner(System.in);
answer = answerscanner.next();
} while (answer.equals("Y") || answer.equals("y"));
}
}
最佳答案
您可以使用while
循环:
Scanner heightscanner = new Scanner(System.in);
System.out.println("Define your height in centimetres.");
while(!heightScanner.hasNextDouble()) {
System.out.println("Invalid Input. Define your height in centimetres.");
heightScanner.nextLine();
}
height = heightscanner.nextDouble();
关于Java - 限制扫描仪仅接受 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393707/