I made a simple Question "system":
Answer
Stores an answer (string) and a boolean value to check if the answer is correct or not.
public class Answer {
private String answer;
private boolean isCorrect;
public Answer(String answer, boolean isCorrect) {
this.answer = answer;
this.isCorrect = isCorrect;
}
public String getAnswer() {
return answer;
}
public boolean isCorrect() {
return isCorrect;
}
}
Question
I don't know if I should use "Answer[] answers" or "Answers... answers" in the Question constructor.
public class Question {
private String question;
private Answer[] answers;
public Question(String question, Answer... answers) {
this.question = question;
this.answers = answers;
}
public String getQuestion() {
return question;
}
public Answer[] getAnswers() {
return answers;
}
public boolean isCorrect(String answer) {
for(Answer tempAnswer : answers)
if(tempAnswer.getAnswer().equals(answer) && tempAnswer.isCorrect())
return true;
return false;
}
public boolean isCorrectIgnoreCase(String answer) {
for(Answer tempAnswer : answers)
if(tempAnswer.getAnswer().equalsIgnoreCase(answer) && tempAnswer.isCorrect())
return true;
return false;
}
}
SAMPLE
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Question questionA = new Question("Do chickens fly?",
new Answer("Yes", true),
new Answer("No", false));
System.out.println(questionA.getQuestion());
while(true) {
if(questionA.isCorrectIgnoreCase(scanner.nextLine()))
break;
else
System.out.println("No, that is incorrect. Please try again.");
}
System.out.println("Correct.");
}