0

我想在问答游戏结束时显示分数。我在两个班上做到了。我的分数代码是:

public class Helper {
    /**
     * This method selects a end game response based on the players score

     * @param numCorrect - number of correct answers
     * @param numRounds - number of rounds
     */
    public static int getResult(int numCorrect, int numRounds){
        //calculate percentage
        int hasil = calculatePercentage(numCorrect, numRounds);
        return hasil;
    }


    /**
     * Calculate the percentage result based on the number correct and number of questions
     * 
     * @param numCorrect - number of questions right
     * @param numRounds - total number of questions
     * @return int percentage correct
     */
    private static int calculatePercentage(int numCorrect, int numRounds) {
        double result = (double)numCorrect/(double)numRounds;
        int percentage = (int) result*100;
        return percentage;
    }

}

public class ResultPretest1 extends Activity implements OnClickListener{
    TextView txtNilai;
    Button tutorial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_pretest1);
        GamePlay currentGame = ((BenkyouApplication)getApplication()).getCurrentGame();
        int nilai = Helper.getResult(currentGame.getRight(), currentGame.getNumRounds());

        txtNilai = (TextView)findViewById(R.id.nilai);
        txtNilai.setText(String.valueOf(nilai));

        tutorial = (Button) findViewById(R.id.tutorial);
        tutorial.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.result, menu);
        return true;
    }

    /*private static int calculatePercentage(final int numCorrect, final int numRounds) {
        int score = 0;
        score = numCorrect/numRounds*100;
        return score;
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()){
        case R.id.tutorial :
            tutorial.setBackgroundResource(R.drawable.tutorial1);

            Intent tulv1 = new Intent(this, TutorialLevel1.class);
            startActivity(tulv1);
            break;
        }


    }
    /* (non-Javadoc)
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     * 
     * This method is to override the back button on the phone
     * to prevent users from navigating back in to the quiz
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK :
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

}

如果您设法正确回答所有问题,则得分为 100,但如果有错误的答案,分数始终显示为 0。如果答案错误,分数不会降低,并且始终为 0。为什么?请帮忙....

4

1 回答 1

0

试试这个:

double result = (((double)numCorrect)/numRounds)*100;
int percentage = (int) result;
return percentage;
于 2013-11-12T07:59:01.263 回答