Java - 限制扫描仪仅接受 double

标签 java input java.util.scanner

好吧,我刚刚开始学习编码,但很快就陷入困境。我有很多问题,而且我是新手,所以我将多次发布我的代码并提出不同的问题。

我的问题:我希望用户在 System.out.println("Define your height in cmimeters."); 之后输入一个 double System.out.println("定义你的体重,以千克为单位。");.我希望我的代码对其进行限制,以便只能输入 double 值,而不能以类似的方式输入任何其他内容,用户不能在程序的第一部分中输入除男性/女性(和派生)之外的任何内容。根据我到目前为止所学到的知识,我可以捕获一个异常。首先,每次尝试时它并不适合我,其次,我希望我的程序能够以一种不会发生异常的方式工作。那可能吗?我实在找不到解决办法。

我从尝试做一个简单的体重指数开始学习。让我们忽略男性和女性值(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/

相关文章:

java - 警告 java.lang.StringIndexOutOfBoundsException : length=75; regionStart=0; regionLength=100

java - 使用页面对象模型时,是否必须为每个页面创建一个类?

java - 如何从 ListView 转到每个列表项的详细 View ?

Python:如何在无限循环运行时从控制台获取输入?

java - 要求输入直到他输入数字?

java - 扫描仪要求我输入两次才能注册一次

java - DBCP 数据库连接未关闭

c - 无法正确终止 "while"循环

Java 扫描器检查字符串还是整数

Java 线程与 Scanner 第一次工作但第二次就不行了?