首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时执行循环,InputMismatchException

同时执行循环,InputMismatchException
EN

Stack Overflow用户
提问于 2015-03-24 02:21:37
回答 2查看 802关注 0票数 1

我对java很陌生,我正在开发一个计算用户BMI的程序。我不明白为什么我的do while循环不适用于我的InputMismatchException。第一次它会说它是不正确的,但是如果你第二次输入它,它就会崩溃。任何帮助都将不胜感激。

代码语言:javascript
复制
import java.util.*;

public class W8
{
   public static void main (String[] args) 
   {
      //Utilities
      Scanner in = new Scanner(System.in);

      //Variables
      double height = 0.0;
      double weight = 0.0;
      boolean error = false;
      double bmi = 0.0;  

      do 
      {
         try 
         {
            error = false;
            while (height <=0)
            {
               System.out.println("Enter height in inches:");
               height = in.nextDouble();
            }
         } 
         catch (InputMismatchException e) 
         {
            in.nextLine();
            System.out.println("Invalid inches value. Must be a decimal number.");
            System.out.println("Re-enter height in inches:");
            height = in.nextDouble();
            error = true;
         } 
      }while (error);

      do
      {
         try
         {
            error = false;
            while (weight <=0)
            {
            System.out.println("Enter weight in pounds:");
            weight = in.nextDouble();
            }
         } 
         catch (InputMismatchException e)
         {

            in.nextLine();
            System.out.println("Invalid pounds value. Must be a decimal number.");
            System.out.println("Re-enter weight in pounds:");
            weight = in.nextDouble();
            error = true;
         } 
      }while (error);

      //bmi calculation
      bmi = (weight/(height*height))*703;

      //Outputs
      System.out.println("Height = " +height+".");
      System.out.println("Weight = " +weight+".");
      System.out.println("Body mass index = " +bmi+ ".");
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-24 03:09:16

试试这个:

公共静态空洞主(String[] args) {

代码语言:javascript
复制
    // Utilities
    Scanner in = new Scanner(System.in);

    // Variables
    double height = 0.0;
    double weight = 0.0;
    boolean error = false;
    double bmi = 0.0;
    System.out.println("Enter height in inches:");
    while (height <= 0) {
        try {
            height = in.nextDouble();
        } catch (InputMismatchException e) {
            height = -1;
            System.out
                    .println("Invalid inches value. Must be a decimal number.");
            System.out.println("Re-enter height in inches:");
            in.nextLine();
            error = true;
        }
    }

    System.out.println("Enter weight in inches:");
    while (weight <= 0) {
        try {
            weight = in.nextDouble();
        } catch (InputMismatchException e) {
            weight = -1;
            System.out
                    .println("Invalid inches value. Must be a decimal number.");
            System.out.println("Re-enter weight in inches:");
            in.nextLine();
            error = true;
        }
    }
    // bmi calculation
    bmi = (weight / (height * height)) * 703;

    // Outputs
    System.out.println("Height = " + height + ".");
    System.out.println("Weight = " + weight + ".");
    System.out.println("Body mass index = " + bmi + ".");
}
票数 0
EN

Stack Overflow用户

发布于 2015-03-24 02:49:42

这在Java中被称为自动类型提升。下面是类型提升的基本规则

类型提升规则

扩大转换不会丢失有关值大小的信息。例如,将int值赋值给一个双变量。这种转换是合法的,因为双倍比ints宽。Java的不断扩大的转换是

从字节到短、int、长、浮点数或双字节 从短到整,长,浮动或双 从一个字符到一个int,一个长的,一个浮点数,或一个双 从一个int到一个长,一个浮动,或一个双 从长到浮动或双倍 从浮动到双倍

double是最宽的基元类型,因为有了这些规则,当您在程序中输入整数时,它们就被提升为double。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29223934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档