[英]Simple Java math operations
I'm making a BMI calculator that doesn't seem to be working. 我正在制作一个似乎无法正常运行的BMI计算器。
I keep getting 0 as my answer, when it should be 15.7 我一直得到0作为答案,应该是15.7
Could someone tell me what I'm doing wrong here? 有人可以告诉我我在做什么错吗?
public class ass10 {
public static void main(String[] args) {
bmi(223,100);
}
public static bmi(int w, int h){
double result;
result = (w/(h*h))*703
System.out.println(result)
}
}
In Java, if you take an int/int, you get an int, rounded to the lower number ( 99/100=0
). 在Java中,如果采用int / int,则将得到一个int,四舍五入到较小的数字(
99/100=0
)。 You want to cast it as a float, or better yet, a double. 您想将其转换为浮点数,或者更好的是将其转换为双精度数。
public static void bmi(int w, int h){
double result;
result = ((double)w/(h*h))*703;
System.out.println(result);
}
I also fixed 2 missing semicolons for you;-) And your function wouldn't work without a return type, so I set that to void. 我还为您修复了2个缺少的分号;-)如果没有返回类型,您的函数将无法工作,因此我将其设置为void。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.