Skip to content

Instantly share code, notes, and snippets.

@witoong623
Created May 5, 2015 01:12
Show Gist options
  • Save witoong623/f03f206a315f4994dbce to your computer and use it in GitHub Desktop.
Save witoong623/f03f206a315f4994dbce to your computer and use it in GitHub Desktop.
Demonstrate simple ask-question
public class Program implements ActionListener
// Store instance of Question clas in array
private Question[] question = new Question[30];
// Maintain question index to indicate current question
private int CurrentQuestion;
........
public Program()
{
// instantiate question 1, assume answer is choice 2
question[0] = new Question("This is question 1",
new String {"choice 1", "choice 2", "choice 3", "choice 4"},
1);
..... do it to 30
// Set current question index
CurrentQuestion = -1;
}
private void MoveNext() {
if (CurrentQuestion < question.Length) {
CurrentQuestion++;
}
else {
return;
}
// I assume every UI variables's name
// Question itself
Label.setText(question[CurrentQuestion].getQuestionText());
// Button that indicate choice
Button1.setText(question[CurrentQuestion].getChoice()[0]);
..... do it to choice 4
}
private void actionPerformed(ActionEvent e)
{
if (e.getSource().getText().equals(
question[CurrentQuestion].getChoice()[question[CurrentQuestion].getAnswer()])) {
// user click correct answer
}
else {
// user click incorrect answer
}
}
public class Question {
private string QuestionText;
private string[] Choice;
private int Answer;
public Quention(string question, string[] choice, int answer) {
QuestionText = question;
Choice = choice;
Answer = answer;
}
public string getQuestionText() {
return QuestionText;
}
public string[] getChoice() {
return Choice;
}
public int getAnswer() {
return Answer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment