在Java中除法结果总是在零(0)? [英] Division in Java always results in zero (0)?
本文介绍了在Java中除法结果总是在零(0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下的功能会从共享preferences,体重和身高的两个值,我用这些来计算BMI, 当我打印值的内容,我得到我的共享preFS已经输入的值(这是好的),但后来当我对它们运行除法运算,我总是得到0,结果.. 哪里的错误?
公众诠释computeBMI(){
共享preferences customShared preference = getShared preferences(
myCustomShared preFS,Activity.MODE_PRIVATE);
弦高= customShared preference.getString(高度preF,);
柱重量= customShared preference.getString(权重preF,);
INT重量=的Integer.parseInt(重量);
INT高=的Integer.parseInt(高);
Toast.makeText(CalculationsActivity.this,身高++重量,Toast.LENGTH_LONG).show();
INT BMI =体重/(身高×高);
返回BMI;
}
解决方案
您正在做的整数师。
您需要转换一个操作数为双
。
The function below gets two values from sharedpreferences, weight and height, and I use these to calculate the BMI, When I print the content of the values I get the values i have entered in the sharedprefs ( which is good) but then when i run a division operation on them, I always get 0 as a result.. Where is the error?
public int computeBMI(){
SharedPreferences customSharedPreference = getSharedPreferences(
"myCustomSharedPrefs", Activity.MODE_PRIVATE);
String Height = customSharedPreference.getString("heightpref", "");
String Weight = customSharedPreference.getString("weightpref", "");
int weight = Integer.parseInt(Weight);
int height = Integer.parseInt(Height);
Toast.makeText(CalculationsActivity.this, Height+" "+ Weight , Toast.LENGTH_LONG).show();
int bmi = weight/(height*height);
return bmi;
}
解决方案
You're doing integer division.
You need to cast one operand to double
.
这篇关于在Java中除法结果总是在零(0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文