I'm using android studio to create a multiple choice question app, and i've coded the quiz but so far it just goes on forever. I want to make it so that once the user has answered 20 questions, whether they are right or wrong, the game ends and sends them to an endscreen that I have created, but so far I can't figure out what to do to make this happen.

this is what i've tried, it doesn't crash the program but it also doesn't actually do anything.

private int round = 0;

round = round + 1;
        if (round == 20) {
            startActivity(new Intent (game.this, endscreen.class));

       }

I'm new to Java so it might be quite a simple solution (sorry if it is)

any ideas?

Presumably you have a loop in which the quesions are asked and answered?
If so just maintain a counter of how many questions and exit the loop after 20,eg (pseudocode)

for (int count = 0; count <20; count++) {
   // do one question
}
endgame

but maybe you have retry loop for incorrect answers for each question, in which case this may work better for you

int count = 0;
mainloop: while true {
   process a question
   every time the user answers...
      if (++count >=20) break mainloop;
}
endgame

Here is the full code, I don't think i've used loops :/

 public class game extends AppCompatActivity {

Button answer1, answer2, answer3, answer4;

ImageButton escape3;

TextView score;

ImageView question;

private Questions mQuestions = new Questions();
private String mAnswer;
private int mScore = 0;
private int mQuestionsLength = mQuestions.mQuestions.length;
private int round = 0;

Random r;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    r = new Random();

    answer1 = (Button) findViewById(R.id.Abutton);
    answer2 = (Button) findViewById(R.id.Bbutton);
    answer3 = (Button) findViewById(R.id.Cbutton);
    answer4 = (Button) findViewById(R.id.Dbutton);
    escape3 = findViewById(R.id.escape_button3);

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

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

            updateQuestion(r.nextInt(mQuestionsLength));
            round = round + 1;
    if (round == 20) {
        startActivity(new Intent (game.this, endscreen.class));

    }

            answer1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (answer1.getText() == mAnswer) {
                        score.setText("Score: " + mScore);
                        mScore++;
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                    }else {
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"wrong", Toast.LENGTH_SHORT).show();

                    }

                }
            });
            answer2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (answer2.getText() == mAnswer) {
                        score.setText("Score: " + mScore);
                        mScore++;
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                    }else {
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"wrong", Toast.LENGTH_SHORT).show();

                    }
                }
            });

            answer3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (answer3.getText() == mAnswer) {
                        score.setText("Score: " + mScore);
                        mScore++;
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                    }else {
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"wrong", Toast.LENGTH_SHORT).show();

                    }

                }
            });

            answer4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (answer4.getText() == mAnswer) {
                        score.setText("Score: " + mScore);
                        mScore++;
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                    } else {
                        updateQuestion(r.nextInt(mQuestionsLength));
                        Toast.makeText(getApplicationContext(),"wrong", Toast.LENGTH_SHORT).show();

                    }
                    }

                });

            escape3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                Intent intent = new Intent(game.this, MainActivity.class);

                startActivity(intent);
                      }

            });

    }

private void updateQuestion(int num) {
            question.setImageResource(mQuestions.getQuestion(num));
            answer1.setText(mQuestions.getChoice1(num));
            answer2.setText(mQuestions.getChoice2(num));
            answer3.setText(mQuestions.getChoice3(num));
            answer4.setText(mQuestions.getChoice4(num));

            mAnswer = mQuestions.getCorrectAnswer(num);
        }

    }

You have the same block of code repeated with only small changes to the variables.
What happens with 20 questions? Do you repeat the same code 20 times? What heppens when you add another question?

That tells you you should be using a loop wth an array answer[] instead of answer1, answer2, answer3 etc.

Refactor the code into a loop, then use either of the solutions I posted earlier.

I have another java file with the questions and answers on in arrays, the questions are image files with the questions on (i'm going to change that but that's a whole other forum lol). When I want to add a question I add another image to my drawable file, then add another line of "A,B,C,D" to my mchoices part, then add whatever the answer is to my mCorrectAnswers part. The "answer1 ect" in my game code refer only to the buttons on the screen of my app that the user presses not the actual answers themselves (there can be more than 4 questions and answers in my code without me having to repeat that code because i've tried with more picture questions before I deleted them).

public class Questions {

public int[] mQuestions = {

        R.drawable.low2,
        R.drawable.low1,

};

private String mChoices [][] = {

        {"A", "B", "C", "D"},
        {"A", "B", "C", "D"}

};

private String mCorrectAnswers[] = {"B","A"};

public int getQuestion(int a){
    int question = mQuestions[a];
    return question;
}

public String getChoice1(int a){
    String choice = mChoices [a][0];
    return choice;
}
public String getChoice2(int a){
    String choice = mChoices [a][1];
    return choice;
}
public String getChoice3(int a){
    String choice = mChoices [a][2];
    return choice;
}
public String getChoice4(int a){
    String choice = mChoices [a][3];
    return choice;
}

public String getCorrectAnswer(int a){
    String answer = mCorrectAnswers[a];
    return answer;
}

}

This latest also repeats almost identical code, which is again very bad design. It should be something like
public int getChoice(int questionNumber, int choiceNumber) {...

In general your program should never contain repeated blocks of code.

But anyway, returning to the original question...

The looping behaviour for questions presumably goes via user clicks and the updateQuestion method.
If so, it's in updateQuestion that you should count the number of questions answered and conditionally go to the end game.

So, i'm starting off by taking out repetetive code like you said and having something like this:

only now the code has errors like "cannot resolve setOnclicklistener"
and "answer" and "mAnswer" are incompatible types saying it expects a button and it instead got a string even though I specify in the code above answer is a button array

final Button answer[] = {answer1, answer2, answer3, answer4};

answer.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    if (answer = mAnswer) {
                        score.setText("Score: " + mScore);
                        mScore++;
                        updateQuestion(r.nextInt(mQuestionsLength));
                        mRound++;
                        Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                    }else {
                        updateQuestion(r.nextInt(mQuestionsLength));
                        mRound++;
                        Toast.makeText(getApplicationContext(),"hey", Toast.LENGTH_SHORT).show();

                    }

                }
            });

That's kinda half-way there. It needs to be more like (incomplete code)

Button answers[] = new Button[4];
for (int i = 0, i<answers.length; i++) {
    answers[i] = ... fimdViewById(... buttonIDs[i] '''
    answers[i].setOnClickListener( ... 
    etc

I tried to fill in the blanks on that code you did there and it didn't work
Could you elaborate on your code?

Button answers[] = new Button[4];
        for (int i = 0, i <answers.length; i++){
            answers[i] = findViewById(R.id.Abutton[i], answer2[i], answer3[i], answer4[i])
            answers[i].setOnClickListener(new View.OnClickListener()
    }
            @Override
            public void onClick(View view) {
                if (answers = mAnswer) {
                    score.setText("Score: " + mScore);
                    mScore++;
                    updateQuestion(r.nextInt(mQuestionsLength));
                    mRound++;
                    Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();

                }else {
                    updateQuestion(r.nextInt(mQuestionsLength));
                    mRound++;

                Toast.makeText(getApplicationContext(),"incorrect", Toast.LENGTH_SHORT).show();

Looking at that code I think I may have been pushing you in a direction that you're not ready for yet. I assumed too much about your familiarity with loops and arrays. If so, I apologise. Maybe its best to go back to the repeated code version and get that working first.

So, going back to the original question, it lookslike you can increment a counter in your updateQuestion method and send the user to the end game when it reaches 20.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.