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

时间:2019-03-18 11:01:36

标签: java android arrays string

大家好,我需要帮助。 我有这个代码
我有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(){

}

如果用户已经回答了10个问题,游戏停止并显示得分,我有50个问题。在该代码中,如果他们回答错误的游戏可以停止,则无法停止,但是如果用户始终正确,则可以加载所有问题

3 个答案:

答案 0 :(得分:1)

在“活动”中添加一个计数器属性

private int numberOfQuestionsAsked = 0;

每个问题问完后,将1加到计数器中

if(answer4.getText().equals(mAnswer)){ //note : use .equals() and not == !
    mScore++;
    numberOfQuestionsAsked++;
    score.setText("Score: " + mScore);
    updateQuestion(r.nextInt(mQuestionLenght));
}

用户回答问题后,检查计数器是否已达到10,如果是,请转到gameOver

if(numberOfQuestionsAsked <= 10) {
    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;

@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().equals(mAnswer)){ //note : use .equals() and not == !
                mScore++;
                score.setText("Score: " + mScore);
                updateQuestion(r.nextInt(mQuestionLenght));
                numberOfQuestionsAsked++;
            } else {
                gameOver();
            }
            if(numberOfQuestionsAsked <= 10) {
                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(){
        numberOfQuestionsAsked = 0;
}

答案 1 :(得分:0)

在您的代码中添加一个计数器,如下所示:

整数计数器= 0;

if(counter <= 10 ){
    updateQuestion(r.nextInt(mQuestionLenght));
    counter++;
} else {
    gameOver();
}

添加并检查,希望它能起作用。

答案 2 :(得分:0)

首先,我将使用:

View.OnClickListener listener = new View.onClickListener() {
    @Override
    public void onClick(View view) {
        if(view instanceOf (TextView) && ((TextView)view).getText().toString().equals(mAnswer)){
            mScore++;
            score.setText("Score: " + mScore);
            if(mScore >= 10) {
                gameCompleted();//ToDo
            } else {
                updateQuestion(r.nextInt(mQuestionLenght));
            }
        } else {
            gameOver();
        }
    }
};

然后,在每个答案中使用此侦听器。 此外,您的随机数可能会失败,因为它可能大于50,并且可能是重复的答案,并且不建议您进行文本比较,可以使用为文本分配ID的对象。

享受编码。