Main Activity - Java

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

package com.example.

ch4practice;

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 was introduced in
1233","Java was created using python","Java has abstract classes?","Java Supports
Interfaces"};
private boolean[] answers = {false,false,false,true,true};
private int score=0;
TextView question;
private int index = 0;
Button yes;
Button no;
TextView ak;
@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);
ak=findViewById(R.id.ak);
question.setText(questions[index]);

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

if (index <= questions.length - 1) {


if (answers[index]) {
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(index <= questions.length-1){


if (!answers[index]) {
score++;
}
index++;
if(index <= questions.length-1)
{

question.setText(questions[index]);
}
else
{
Toast.makeText(MainActivity.this, "Your Score Is"+" " + score,
Toast.LENGTH_SHORT).show();
}
}

}
});
}
}

You might also like