Contents

Kata 6 solution

You want a method that takes arguments for a question, and the correct answer. The method needs to ask the question to the user, and record the user’s answer. The user’s answer then ought to be compared to the correct answer to determine if it was correct or not.

You can start out with the signature and an empty body:

static bool AskQuestion(string question, string correctAnswer) {
}

Next, add the code the ask the question and record the answer:

static bool AskQuestion(string question, string correctAnswer) {
    Console.WriteLine(question);
    string answer = Console.ReadLine();
}

After that, add an if statement to check if the answer is correct or incorrect:

static bool AskQuestion(string question, string correctAnswer) {
    Console.WriteLine(question);

    string answer = Console.ReadLine();

    if(answer == correctAnswer) {
        Console.WriteLine("Correct!");
    }
    else {
        Console.WriteLine("Incorrect!");
    }
}

Finally, return true or false depending on the answer in the if statement:

static bool AskQuestion(string question, string correctAnswer) {
    Console.WriteLine(question);

    string answer = Console.ReadLine();

    if(answer == correctAnswer) {
        Console.WriteLine("Correct!");
        return true;
    }

    Console.WriteLine("Incorrect!");
    return false;
}

Notice the else is now redundant - if the answer is correct, the return means the method is exited. Therefore the last lines will always be run if the answer is incorrect.

Finally, you can call AskQuestion like so:

bool questionIsCorrect = AskQuestion("Traditionally, how many 'Wonders of the World' are there?", "7");
Copyright Mikiel Agutu 2019