Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication;
- 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 {
- Button btnch1, btnch2, btnch3;
- TextView tvquestion, tvscore;
- QuestionBank qb = new QuestionBank();
- int questionNo = 0;
- String correct;
- int score = 0;
- int count = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvquestion = findViewById(R.id.tvQuestion);
- tvscore = findViewById(R.id.tvscore);
- btnch1 = findViewById(R.id.btn1);
- btnch2 = findViewById(R.id.btn2);
- btnch3 = findViewById(R.id.btn3);
- updateQuestion();
- btnch1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- if(btnch1.getText() == correct){
- score = score +1;
- updateScore();
- updateQuestion();
- count++;
- Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
- }
- else{
- Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
- }
- }
- catch (Exception e){
- if (count >= 3){
- }
- }
- }
- });
- btnch2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- if(btnch2.getText() == correct){
- score = score +1;
- updateScore();
- updateQuestion();
- Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
- }
- else{
- Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
- }
- }
- catch (Exception e){
- }
- }
- });
- btnch3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- if(btnch3.getText() == correct){
- score = score +1;
- updateScore();
- updateQuestion();
- Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
- }
- else{
- Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
- }
- }
- catch (Exception e){
- }
- }
- });
- }
- public void updateQuestion(){
- tvquestion.setText(qb.getQuestion(questionNo));
- btnch1.setText(qb.getChoice1(questionNo));
- btnch2.setText(qb.getChoice3(questionNo));
- btnch3.setText(qb.getChoice3(questionNo));
- correct = qb.getCorrectants(questionNo);
- questionNo++;
- }
- public void updateScore(){
- tvscore.setText("Score: "+score);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement