Question Answer In Android Studio

 package com.example.ch4practiceset;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private String[] questions = {"Java is a person?" , "Java is introduced in 1233? " , "java is created with Python?", "Java has a Abstact classes" , "Java Supporte interface?"};
private boolean[] answers = {false , false , false , true , true};
private int score = 0;
Button yes;
Button no;
TextView question;

private int index= 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
question = findViewById(R.id.question);
question.setText(questions[index]);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (index<=questions.length - 1){
//If you have given correct answer
if (answers[index] == true){
score++;
}
index ++;
if (index<=questions.length - 1){
question.setText(questions[index]);
}
else{
Toast.makeText(MainActivity.this, "Your score is:" + score, Toast.LENGTH_SHORT).show();
}
}
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If the array is not going out of bound
if (index<=questions.length - 1){
//If you have given correct answer
if (answers[index] == false){
score++;
}
index ++;
if (index<=questions.length - 1){
question.setText(questions[index]);
}
else{
Toast.makeText(MainActivity.this, "Your score is:" + score, Toast.LENGTH_SHORT).show();
}
}
}
});

}
}

Comments