Java - 限制扫描程序只接受双打

时间:2016-04-04 02:26:05

标签: java input java.util.scanner

好的,所以我刚开始学习编码并且卡住了很快。我有很多问题而且我是新来的,所以我会用不同的问题多次发布我的代码。

我的问题:我希望用户在doubleSystem.out.println("Define your height in centimetres.");之后输入System.out.println("Define your weight in kilograms.");。我希望我的代码限制它,以便只能以类似的方式输入double和其他任何东西,用户不能在程序的第一部分输入除男/女(和派生)之外的任何内容。从我到目前为止学到的东西,我可以发现一个例外。首先,每次我尝试它都不适用于我,其次,我希望我的程序以某种方式工作,甚至不会发生异常。那可能吗?我真的找不到解决方案。

我开始尝试做一个简单的BMI测量仪。让我们忽略一个事实,即男性和女性的价值是相同的,我将在未来改变它。 这是我的代码:

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"));
 }
}

1 个答案:

答案 0 :(得分:1)

您可以使用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();