아래 함수는 sharedpreferences, weight 및 height에서 두 가지 값을 가져오고 BMI를 계산하기 위해이 값을 사용합니다. 값의 내용을 인쇄 할 때 sharedprefs에 입력 한 값을 얻었지만 내가 그들에 나눗셈 연산을 실행할 때, 결과적으로 항상 0이됩니다. 어디서 오류가 있습니까?Java의 분할은 항상 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을 곱하여 부동 소수점으로 만듭니다. –