There is a "Question" class which are designed to create questions for users.
checkAnswer method of the Question class is designed for taking response which does not take into account different spaces or upper/lowercase characters. For example, the response "abcd" should match an answer of "A b Cd".
/** A question with a text and an answer. */
public class Question
{
private String text;
private String answer;
/** Constructs a question with empty question and answer. */
public Question()
{
text = "";
answer = "";
}
/** Sets the question text. @param questionText the text of this question */
public void setText(String questionText)
{
text = questionText;
}
/** Sets the answer for this question. @param correctResponse the answer */
public void setAnswer(String correctResponse)
{
answer = correctResponse;
}
/** Checks a given response for correctness. @param response the response to check @return true if the response was correct, false otherwise */
public boolean checkAnswer(String response)
{
response = response.toLowerCase();
for(int i = 0; i <= response.length() - 1; i++)
{
if(response.charAt(i) == ' ')
{
response = response.subString(0, i) +
response.subString(i, response.length());
}
}
response.equals(answer.defaultAnswer());
}
/** Displays this question. */
public void display()
{
System.out.println(text);
}
public void displayAnswer()
{
System.out.println(answer);
}
private String defaultAnswer()
{
answer = answer.toLowerCase();
for(int i = 0; i <= answer.length() - 1; i++)
{
if(answer.charAt(i) == ' ')
{
answer = answer.subString(0, i) +
answer.subString(i, answer.length());
}
}
}
}