1h1h1h1h1h1h1h:
私のアプリは、ユーザーがトーストとして、すべての質問に答えた後、正しい得た質問のパーセントを出してくれる部分があります、その中に、クイズアプリです。
トーストが現れているが、割合は、常に0として来ています。
私はインフロント、いくつかのログメッセージを持っています:
Log.i("MainActivity", "Amount i got right "+Integer.toString(right));
Log.i("MainActivity", "total is "+Integer.toString(total));
Toast.makeText(this, "You answered " + (right/total)*100 + "% of questions correct", Toast.LENGTH_SHORT).show();
ログでは、「I / MainActivityを:私が得た金額右4の合計は6である」と言います
なぜトースト率は0として来ています?
ここでの機能があります:
int i = 0;
int total = mQuestionBank.length;
check = true;
right = 0;
while (i<total && check){
if(mQuestionBank[i].isAlreadyAnswered()){
if(mQuestionBank[i].isAnswerTrue()){
right+=1;
check = true;
}
}else{
check = false;
}
i++;
}
if(check) {
double percent = (right / total) * 100;
Log.i("MainActivity", "Amount i got right "+Integer.toString(right));
Log.i("MainActivity", "total is "+Integer.toString(total));
Toast.makeText(this, "You answered " + (right/total)*100 + "% of questions correct", Toast.LENGTH_SHORT).show();
}else {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
mTrueButton.setEnabled(!mQuestionBank[mCurrentIndex].isAlreadyAnswered());
mFalseButton.setEnabled(!mQuestionBank[mCurrentIndex].isAlreadyAnswered());
}
トーストは、「あなたは正しい質問の0%に答え」と言います
Sudipta Basak:
コードはOKです。あなただけの簡単な変更を必要としています。これを試して :
double percent = (right*100)/total ;
または、
double percent = ((double)right/total)*100 ;
これは動作します願っています。
アップデート:
なぜあなたのコードは働いていませんでしたか?
一例として取るright = 5
とtotal = 10
。変数右と真として整数がそうされright/total
た後、彼らは整数値と値を返しますので、常に0ゼロになる.
整数値では考慮されていません。問題を解決するには、二重変数として権利と合計を取るか、またはdoubleとして権利をキャストすることができます。そして、最初に説明した式。***のでright*100 = 500
と(right*100)/total = 500/10 = 50
。