这是我的代码.由于某种原因,我的BMI计算不正确.当我在计算器上检查输出时:(10/((10/100)^2)))我得到1000,但在我的程序中,我得到5.我不确定我做错了什么.这是我的代码:
import javax.swing.*;
public class BMI {
public static void main(String args[]) {
int height;
int weight;
String getweight;
getweight = JOptionPane.showInputDialog(null, "Please enter your weight in Kilograms");
String getheight;
getheight = JOptionPane.showInputDialog(null, "Please enter your height in Centimeters");
weight = Integer.parseInt(getweight);
height = Integer.parseInt(getheight);
double bmi;
bmi = (weight/((height/100)^2));
JOptionPane.showMessageDialog(null, "Your BMI is: " + bmi);
}
}
Run Code Online (Sandbox Code Playgroud)
WuH*_*ted 119
^在java中并不意味着提升到一种力量.这意味着XOR.
你可以使用java的 Math.pow()
你可能想考虑使用double而不是int-that:
double height;
double weight;
Run Code Online (Sandbox Code Playgroud)
请注意,199/100评估为1.
A.G*_*AYS 24
我们可以用
Math.pow(2, 4);
Run Code Online (Sandbox Code Playgroud)
这意味着2到4的功率(2 ^ 4)
答案= 16
你的计算可能是罪魁祸首.尝试使用:
bmi = weight / Math.pow(height / 100.0, 2.0);
Run Code Online (Sandbox Code Playgroud)
因为这两个height和100是整数,将当你有可能得到错误的答案.但是,100.0是双倍的.我建议你做weight一个双.此外,^运营商不是为了权力.请改用该Math.pow()方法.
^不是你想要的运营商。您正在寻找 的pow方法java.lang.Math。
您可以使用Math.pow(value, power).
例子:
Math.pow(23, 5); // 23 to the fifth power
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
350569 次 |
| 最近记录: |