【问题标题】:Java Data Type Exercise troubleshooting [duplicate]Java数据类型练习疑难解答[重复]
【发布时间】:2018-03-20 18:12:27
【问题描述】:

我在显示体重指数计算结果时遇到问题。我不确定为什么 bodyMassIndex 变量没有执行计算并显示结果。感谢您的任何反馈。

// Write a Java program to compute body mass index (BMI).

public class Exercise3 {
  public static void main (String [] args) {

// Variable Declaration
int height;
int weight;
double bodyMassIndex;
Scanner input = new Scanner (System.in);

// Obtain user input
System.out.println("What is your remaining height in inches: ");
height = input.nextInt();

System.out.println("What is your weight (in pounds): ");
weight = input.nextInt();

// Perform computations

bodyMassIndex = (weight / (height * height)) * 703;

// Display Results
System.out.println("Your Body Mass Index is: " + bodyMassIndex);

  }
}

【问题讨论】:

  • 为什么 bodyMassIndex 变量没有执行计算并显示结果——您预计会发生什么,正在发生什么?你认为这是为什么?您是否尝试过将 heightweight 定义为 double (所以您不只是在进行整数运算,然后分配给双精度数)

标签: java


【解决方案1】:

您应该将所有整数类型转换为双精度数,以便获得结果。

事实上这对你有用

bodyMassIndex = ((double)weight / (height * height)) * 703;

【讨论】:

  • 这行得通,但你只需要在weight 上进行double 强制转换——其他都是多余的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多