如何完成数组问题,它总是重复

我有这个代码

,我有50个问题字符串,我想如果已经出现了10个问题,那么游戏就结束了。感谢您的帮助


private Question mQuestion = new Question();


private String mAnswer;

private int mScore = 0;

private int mQuestionLenght = 5 ;


Random r;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_quiz);


    r = new Random();



    answer1 = (Button) findViewById(R.id.answer1);

    answer2 = (Button) findViewById(R.id.answer2);

    answer3 = (Button) findViewById(R.id.answer3);

    answer4 = (Button) findViewById(R.id.answer4);


    score = (TextView) findViewById(R.id.score);

    question = (TextView) findViewById(R.id.question);

    score.setText("Score: " + mScore  );


    updateQuestion(r.nextInt(mQuestionLenght));



    answer4.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            if(answer4.getText() == mAnswer){

                mScore++;

                score.setText("Score: " + mScore);

                updateQuestion(r.nextInt(mQuestionLenght));

            } else {

                gameOver();

            }

        }

    });

}

private void updateQuestion(int num){

    question.setText(mQuestion.getQuestion(num));

    answer1.setText(mQuestion.getChoice1(num));

    answer2.setText(mQuestion.getChoice2(num));

    answer3.setText(mQuestion.getChoice3(num));

    answer4.setText(mQuestion.getChoice4(num));

    mAnswer = mQuestion.getCorrectAnswer(num);


}

private void gameOver(){

}


我有50个问题,我想如果用户已经回答10个问题游戏停止并显示分数。在该代码中,如果他们错误的答案游戏可以停止,它不能停止,但如果用户总是正确的游戏加载所有问题


繁星coding
浏览 93回答 3
3回答

幕布斯7119047

在 Acitivty 中,添加计数器属性private int numberOfQuestionsAsked = 0;在提出每个问题后,将1添加到您的计数器if(answer4.getText().equals(mAnswer)){ //note : use .equals() and not == !&nbsp; &nbsp; mScore++;&nbsp; &nbsp; numberOfQuestionsAsked++;&nbsp; &nbsp; score.setText("Score: " + mScore);&nbsp; &nbsp; updateQuestion(r.nextInt(mQuestionLenght));}用户回答问题后,检查计数器是否达到10,如果是,请转到gameOverif(numberOfQuestionsAsked <= 10) {&nbsp; &nbsp; gameOver();}在gameOver中,重置计数器,以便游戏可以重新启动numberOfQuestionsAsked = 0;您的代码应如下所示private Question mQuestion = new Question();private String mAnswer;private int mScore = 0;private int mQuestionLenght = 5 ;private int numberOfQuestionsAsked = 0;Random r;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_quiz);&nbsp; &nbsp; r = new Random();&nbsp; &nbsp; answer1 = (Button) findViewById(R.id.answer1);&nbsp; &nbsp; answer2 = (Button) findViewById(R.id.answer2);&nbsp; &nbsp; answer3 = (Button) findViewById(R.id.answer3);&nbsp; &nbsp; answer4 = (Button) findViewById(R.id.answer4);&nbsp; &nbsp; score = (TextView) findViewById(R.id.score);&nbsp; &nbsp; question = (TextView) findViewById(R.id.question);&nbsp; &nbsp; score.setText("Score: " + mScore&nbsp; );&nbsp; &nbsp; updateQuestion(r.nextInt(mQuestionLenght));&nbsp; &nbsp; answer4.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(answer4.getText().equals(mAnswer)){ //note : use .equals() and not == !&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mScore++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.setText("Score: " + mScore);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateQuestion(r.nextInt(mQuestionLenght));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberOfQuestionsAsked++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOver();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(numberOfQuestionsAsked <= 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOver();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void updateQuestion(int num){&nbsp; &nbsp; question.setText(mQuestion.getQuestion(num));&nbsp; &nbsp; answer1.setText(mQuestion.getChoice1(num));&nbsp; &nbsp; answer2.setText(mQuestion.getChoice2(num));&nbsp; &nbsp; answer3.setText(mQuestion.getChoice3(num));&nbsp; &nbsp; answer4.setText(mQuestion.getChoice4(num));&nbsp; &nbsp; mAnswer = mQuestion.getCorrectAnswer(num);}private void gameOver(){&nbsp; &nbsp; &nbsp; &nbsp; numberOfQuestionsAsked = 0;}

jeck猫

首先,我会使用:View.OnClickListener listener = new View.onClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; if(view instanceOf (TextView) && ((TextView)view).getText().toString().equals(mAnswer)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mScore++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score.setText("Score: " + mScore);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mScore >= 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameCompleted();//ToDo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateQuestion(r.nextInt(mQuestionLenght));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOver();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }};然后,在每个答案中使用此侦听器。此外,您的随机数可能会失败,因为它可能高于50,并且可以是重复的答案,并且不建议使用文本比较,您可以使用为文本分配ID的对象。享受编码。

慕村9548890

在代码中添加一个计数器,如下所示:整型计数器 = 0;if(counter <= 10 ){&nbsp; &nbsp; updateQuestion(r.nextInt(mQuestionLenght));&nbsp; &nbsp; counter++;} else {&nbsp; &nbsp; gameOver();}添加此内容并检查,希望它能正常工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java