Java 我的扫描仪跳过下一个双精度和下一个整数?
每当我运行扫描仪时,它会在称重后跳过高度(双)环。它还可以跳过年龄后的级别(int)循环 这是我的扫描器课 导入java.util.ScannerJava 我的扫描仪跳过下一个双精度和下一个整数?,java,Java,每当我运行扫描仪时,它会在称重后跳过高度(双)环。它还可以跳过年龄后的级别(int)循环 这是我的扫描器课 导入java.util.Scanner public class HowHealthy { public static void main(String[] args) { String aGender = ""; //New healthy objec tis created Healthy person = new Healthy(); /
public class HowHealthy
{
public static void main(String[] args)
{
String aGender = "";
//New healthy objec tis created
Healthy person = new Healthy();
//Scanner object is created
Scanner in = new Scanner(System.in);
String name = "";
while(!person.setName(name))
{
System.out.print("Person's name: ");
name = in.nextLine();
if(!person.setName(name))
{
System.out.println("Invalid name - must be at least one character!");
}
}
char gender = '\0';
while(!person.setGender(gender))
{
System.out.print(name + ", are you male of female (M/F): ");
gender = in.nextLine().toUpperCase().charAt(0);
if(!person.setGender(gender))
{
System.out.println("Invalid gender - must be M or F (upper or lower case)":
}
}
double weight = 0;
while(!person.setWeight(weight))
{
System.out.print(name + "'s weight (pounds): ");
weight = in.nextDouble();
in.nextLine();
if(!person.setWeight(weight))
{
System.out.println("Invalid weight - must be at least 100 pounds!");
}
}
double height = 0;
while(!person.setHeight(height))
{
System.out.print(name + "'s height (inches): ");
height = in.nextDouble();
if(!person.setHeight(height))
{
System.out.println("Invalid height - must be 60..84, inclusively!");
}
}
int age = 0;
while(!person.setAge(age))
{
System.out.print(name + "'s age (years): ");
age = in.nextInt();
in.nextLine();
if(!person.setAge(age))
{
System.out.println("Invalid age - must be at least 18!");
}
}
System.out.println();
System.out.println("Activity Level: Use these categories:");
System.out.println("\t1 - Sedentary (little or no exercise, desk job)");
System.out.println("\t2 - Lightly active (little exercise / sports 3-5 days/wk");
System.out.println("\t3 - Moderately active(moderate exercise / sports 3-5
System.out.println("\t4 - Very active (hard exercise / sports 6 -7 day/wk)");
System.out.println("\t5 - Extra active (hard daily exercise / sports \n\t physica2X)
int level = 0;
while(!person.setLevel(level))
{
System.out.print("How active are you? ");
level = in.nextInt();
if(!person.setLevel(level))
{
System.out.println("Invalid acitvity level - must be 1..5, inclusively!");
}
}
System.out.println();
//Creates a new Healthy object and prints values based on user's input
System.out.println(person.getName()+ "'s information");
System.out.printf("Weight: %.1f %s \n", person.getWeight(), "pounds");
System.out.printf("Height: %.1f %s \n", person.getHeight(), "inches");
System.out.println("Age: " + person.getAge() + " years");
if (gender == 'M')
{
aGender = "male";
}
else
{
aGender = "female";
}
System.out.println("These are for a " + aGender);
System.out.println();
//Calculates the person's BMR, BMI and TDEE based on user input
System.out.printf("BMR is %.2f \n", person.calcBMR());
System.out.printf("BMI is %.2f \n", person.calcBMI());
System.out.printf("TDEE is %.2f \n", person.calcTDEE());
//Determines person's weight status based on BMI calculated
double BMI = person.calcBMI();
//Displays person's weight status
System.out.println("Your BMI classifies you as " + person.calcWeightStatus());
}
}
这是我的扫描器课 在这两种情况下,在.nextLine()中执行
操作后,.nextLine()中缺少
。如果所有其他代码行都使用类似于.nextDouble()
中的in.nextLine()
然后是.nextLine()中的in.nextLine()
的东西工作,我猜这就是缺少的地方。因为它完全跳过了循环,所以setHeight()和setLevel()的方法有问题
while(!person.setHeight(height))
若它跳过这个循环,那个么它一定意味着setHeight(height)在返回false时返回true,或者您需要去掉“!” in.nextLine()不起作用。我建议大家看看Sotirios Delimanolis发布的内容,因为他们似乎遇到了类似的问题。看起来可能是换行符“/n”造成的。