0

クイズゲーム終了時にスコアを表示したい。2クラスで作っています。スコアの私のコードは次のとおりです。

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 に答える