Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     Button btnch1, btnch2, btnch3;
  14.     TextView tvquestion, tvscore;
  15.     QuestionBank qb = new QuestionBank();
  16.     int questionNo = 0;
  17.     String correct;
  18.     int score = 0;
  19.     int count = 0;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.  
  26.         tvquestion = findViewById(R.id.tvQuestion);
  27.         tvscore = findViewById(R.id.tvscore);
  28.         btnch1 = findViewById(R.id.btn1);
  29.         btnch2 = findViewById(R.id.btn2);
  30.         btnch3 = findViewById(R.id.btn3);
  31.         updateQuestion();
  32.  
  33.         btnch1.setOnClickListener(new View.OnClickListener() {
  34.             @Override
  35.             public void onClick(View view) {
  36.                 try {
  37.                     if(btnch1.getText() == correct){
  38.                         score = score +1;
  39.                         updateScore();
  40.                         updateQuestion();
  41.                         count++;
  42.                         Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
  43.                     }
  44.                     else{
  45.                         Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
  46.                     }
  47.                 }
  48.                 catch (Exception e){
  49.  
  50.                     if (count >= 3){
  51.                        
  52.                     }
  53.  
  54.                 }
  55.  
  56.             }
  57.         });
  58.  
  59.  
  60.         btnch2.setOnClickListener(new View.OnClickListener() {
  61.             @Override
  62.             public void onClick(View view) {
  63.                 try {
  64.                     if(btnch2.getText() == correct){
  65.                         score = score +1;
  66.                         updateScore();
  67.                         updateQuestion();
  68.                         Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
  69.                     }
  70.                     else{
  71.                         Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
  72.                     }
  73.                 }
  74.  
  75.  
  76.                 catch (Exception e){
  77.  
  78.                 }
  79.             }
  80.         });
  81.  
  82.         btnch3.setOnClickListener(new View.OnClickListener() {
  83.             @Override
  84.             public void onClick(View view) {
  85.                 try {
  86.                     if(btnch3.getText() == correct){
  87.                         score = score +1;
  88.                         updateScore();
  89.                         updateQuestion();
  90.                         Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT);
  91.                     }
  92.                     else{
  93.                         Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT);
  94.                     }
  95.                 }
  96.  
  97.  
  98.                 catch (Exception e){
  99.  
  100.                 }
  101.             }
  102.         });
  103.  
  104.  
  105.  
  106.  
  107.     }
  108.  
  109.     public void updateQuestion(){
  110.         tvquestion.setText(qb.getQuestion(questionNo));
  111.         btnch1.setText(qb.getChoice1(questionNo));
  112.         btnch2.setText(qb.getChoice3(questionNo));
  113.         btnch3.setText(qb.getChoice3(questionNo));
  114.         correct = qb.getCorrectants(questionNo);
  115.         questionNo++;
  116.     }
  117.  
  118.     public void updateScore(){
  119.         tvscore.setText("Score: "+score);
  120.     }
  121.  
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement