【发布时间】:2012-05-04 20:25:37
【问题描述】:
下面的函数从 sharedpreferences 中获取两个值,即体重和身高,我使用这些值来计算 BMI, 当我打印值的内容时,我得到了我在 sharedprefs 中输入的值(这很好)但是当我对它们运行除法运算时,结果总是得到 0.. 哪里出错了?
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;
}
【问题讨论】:
-
你在做整数除法。乘以 1.0 使其成为浮点数。