在Java中将数字提高到幂

2022-08-31 10:42:07

这是我的代码。由于某种原因,我的BMI计算不正确。当我在计算器上检查输出时:我得到1000,但在我的程序中,我得到5。我不确定我做错了什么。这是我的代码:(10/((10/100)^2)))

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

答案 1

^在java中并不意味着提升到一种力量。它的意思是异或。

你可以使用java的Math.pow()


您可能要考虑使用而不是 - 即:doubleint

double height;
double weight;

请注意,计算结果为 1。199/100


答案 2

我们可以使用

Math.pow(2, 4);

这个均值 2 到 4 的幂 (2^4)

答案 = 16


推荐