用Java增加数字 [英] Raising a number to a power in Java

查看:99
本文介绍了用Java增加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my code. For some reason my BMI is not calculated correctly. When I check the output on a calculator for this : (10/((10/100)^2))) I get 1000, but in my program, I get 5. I'm not sure what I am doing wrong. Here is my code:

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


推荐答案

< java中的code> ^ 并不意味着提高功率。这意味着XOR。

^ in java does not mean to raise to a power. It means XOR.

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

You can use java's Math.pow()

您可能要考虑使用 double 而不是 int -that is:

And you might want to consider using double instead of int—that is:

double height;
double weight;

请注意 199/100 评估为1 。

这篇关于用Java增加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆