参见英文答案 > Double value returns 0 3个
下面的函数从sharedpreferences,weight和height得到两个值,我用这些来计算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;}
解决方法:
你正在进行整数除法.
你需要将一个 *** 作数转换为double.
总结以上是内存溢出为你收集整理的Java中的除法总是导致零(0)?全部内容,希望文章能够帮你解决Java中的除法总是导致零(0)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)