2017-12-28 80 views
0

私は、1つの変数intを次のアクティビティの文字列に渡し、次のアクティビティでその文字列を受け取り、もう一度intを返します。私はパーセンテージを計算し、テキストビューと過去の変数と約1を計算して、空でないかどうかを確認してから、パーセント(例:3/45)* 100)を計算し、テキストビューで表示します。文字列...しかし、私は間違いを何らかの形ですることができますか?あなたがそうのようなif文の後に{}持っている必要があります%を計算し、アンドロイドスタジオtexviewに表示

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

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が整数であるので、あなたが45で割ったときに整数を取得することを指摘しパーセンテージではありません。

この問題を解決するには、除算を計算する前にnum3をdoubleまたはnum3または45のいずれかをdoubleにキャストします。例えば

、単純な修正は以下にライン4を変更することが挙げられるであろう:

result = Math.round ((num3/45.0) * 100); 
関連する問題