FORUMS

Quiz error

3 posts
Thanks Meter: 0
 
By yabi54, Junior Member on 26th May 2020, 06:35 PM
Post Reply Email Thread
Hello everyone! I have just made a program in Android Studio which should be a Quiz. If you could help me to find the mistake in my code I would be very grateful. Probably it's just a little mistake (as I am beginner...).

At the beginning I have created an arraylist, that does contain the strings a,b,c,d. Then, the program should make a few questions, that can be answered with Yes or No. If the user clicks the button yes, some strings are going to be removed from the Arraylist. This should happen until only one string is in the arraylist.

Here is my code:
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Arrays;
import java.util.Collections;
import java.util.*;

public class MainActivity extends AppCompatActivity {
static ArrayList<String> p = new ArrayList<String>();
static Button endbutton;

static Button questionbutton;
static Button yesbutton;
static Button nobutton;
static boolean yesbuttonisclicked=false;
static boolean nobuttonisclicked=false;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

endbutton = findViewById(R.id.endbutton);


questionbutton = findViewById(R.id.questionbutton);
yesbutton = findViewById(R.id.yesbutton);
nobutton = findViewById(R.id.nobutton);

p.add("a");
p.add("b");
p.add("c");
p.add("d");

yesbutton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
yesbuttonisclicked=true;
nobuttonisclicked=false;
}
});
nobutton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
nobuttonisclicked=true;
yesbuttonisclicked=false;
}
});

List array1 = Arrays.asList("1", "2", "3", "4");
Collections.shuffle(array1);

int i=0;
if (array1.get(i) == "1") {
questionbutton.setText("Question1");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("b");

if (p.size() == 1) {
endbutton.setText(p.get(0));
}

} else {
p.remove("c");
p.remove("d");

if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("2")) {
questionbutton.setText("Question2");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("c");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("b");
p.remove("d");
if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("3")) {
questionbutton.setText("Question3");
if (yesbuttonisclicked) {
p.remove("a");
p.remove("d");


if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("c");
p.remove("b");

if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
} else if (array1.get(i).equals("4")) {
questionbutton.setText("Question4");
if (yesbuttonisclicked) {
p.remove("d");
p.remove("c");


if (p.size() == 1) {
endbutton.setText(p.get(0));
}
} else {
p.remove("a");
p.remove("b");

if (p.size() == 1) {
endbutton.setText(p.get(0));
}
}
}
}
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes