0

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

public class Helper {

    /**
     * This method selects a end game response based on the players score

    /**
     * Method to return an image to use for the end of game screen
     * 
     * @param numCorrect - number of correct answers
     * @param numRounds - number of rounds
     */
    public static int getResult(int numCorrect, int numRounds){
    //calculate percentage
    int percentage = calculatePercentage(numCorrect, numRounds);
    return percentage;
}


/**
 * 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) {
    int percentage = numCorrect/numRounds*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);
    }

}

但是显示的分数总是0。当答案正确时分数没有增加请帮助我....

4

2 回答 2

0

这可能与您在Helper. 整数除法会截断小数,所以当你有

int percentage = numCorrect/numRounds*100;

然后它将始终返回 0,除非他们 100% 正确。尝试使用doubleorfloat代替int.

于 2013-11-11T15:33:30.080 回答
0

首先尝试乘法如下: int percentage = (100*numCorrect)/numRounds;

于 2015-04-03T22:40:20.783 回答