2017-12-28 10 views
0

나는 하나의 변수 int를 다음 활동에 대한 문자열을 통해 전달합니다. 다음 활동에서 그 문자열을 가져 와서 다시 int와 int를 반환합니다. 백분율과 디스플레이 및 텍스트 뷰, 과거 변수 및 approx1을 계산하여 비어 있지 않은지 확인한 다음 백분율 ex : ((3/45) * 100)을 계산하고 텍스트 뷰에 표시하여 다시 검토합니다. 문자열 ...하지만 어떤 식 으로든 나는 실수를 할 수 있습니다.퍼센트를 계산하고 texview android studio에 표시합니다.

Bundle bundle = getIntent(). GetExtras(); 
    String aprox1 = bundle.getString ("aprox1"); 
    if (aprox1! = null) 
    try { 
    num3 = Integer.parseInt (aprox1); 
     result = Math.round ((num3/45) * 100); 
     TextView counter2 = (TextView) findViewById(R.id.textView16); 
    String abcString2 = Integer.toString (result); 
    counter2.setText (abcString2); 
    } 
     catch (NumberFormatException e) { 
     } 

답변

1

당신은 후 {}가 필요가있는 경우 성명과 같이 :

num3는 정수이기 때문에 당신이 45를 나눌 때 당신이 정수를 얻을 것이다 주목할 가치가
if (aprox1! = null) { 
    try { 
    num3 = Integer.parseInt (aprox1); 
    result = Math.round ((num3/45) * 100); 
    TextView counter2 = (TextView) findViewById(R.id.textView16); 
    String abcString2 = Integer.toString (result); 
    counter2.setText (abcString2); 
    } 
    catch (NumberFormatException e) { 
    } 
} 

비율이 아닙니다.

이 문제를 해결하려면 나누기를 계산하기 전에 num3을 double 또는 num3 또는 45를 double로 변환하십시오.

result = Math.round ((num3/45.0) * 100); 
:

예를 들어, 간단한 수정은 다음 라인 (4)를 변경하는 것이 포함

관련 문제