Contents

Kata 5 solution

Q1

Using the OR operator, amend the if statements that check correct answers. For instance:

Console.WriteLine("What year did the Titanic sink?");
string answerOne = Console.ReadLine();

if (answerOne == "1912" || answerOne == "nineteen twelve") {
    Console.WriteLine("Correct!");
}
else {
    Console.WriteLine("Incorrect!");
}

Q2

This is more involved, and requires some head scratching!

First we need bool variable to track if the answers to the questions are correct.

You can do this by declaring a bool above the if statement for each question. You can assign it true or false in each block. For instance:

Console.WriteLine("What year did the Titanic sink?");
string answerOne = Console.ReadLine();
bool answerOneCorrect;

if (answerOne == "1912") {
    Console.WriteLine("Correct!");
    answerOneCorrect = true;
}
else {
    Console.WriteLine("Incorrect!");
    answerOneCorrect = false;
}

You can do the same for question two.

The next thing is to create if statements that print a message based on which answers were correct.

The first condition is that both answers were incorrect. Use AND:

if (answerOneCorrect == false && answerTwoCorrect == false) {
    Console.WriteLine("Both questions were wrong!");
}

If both answers were correct, we can use another if statement that looks like this:

if (answerOneCorrect == true && answerTwoCorrect == true) {
    Console.WriteLine("Both questions were right!");
}

We can also output messages if either question one or question two were incorrect:

if (answerOneCorrect == false) {
    Console.WriteLine("question one was wrong!");
}

if (answerTwoCorrect == false) {
    Console.WriteLine("question two was wrong!");
}

We want to display only a single summary message. To do this we’ll need to use some else blocks.

First, let’s create some variables to make life easier. Create a variable that stores if both answers were incorrect:

bool bothAnswersIncorrect = answerOneCorrect == false && answerTwoCorrect == false;

The first if statement can then be:

if (bothAnswersIncorrect == true) {
    Console.WriteLine("Both questions were wrong!");
}

We can then add an else clause to include our other if statements:

bool bothAnswersIncorrect = answerOneCorrect == false && answerTwoCorrect == false;

if (bothAnswersIncorrect == true) {
    Console.WriteLine("Both questions were wrong!");
}
else {
    if (answerOneCorrect == false) {
        Console.WriteLine("question one was wrong!");
    }
    
    if (answerTwoCorrect == false) {
        Console.WriteLine("question two was wrong!");
    }
}

Finally, we need a condition to use when both answers are correct. We can use yet another if - else block to achieve this:

bool bothAnswersIncorrect = answerOneCorrect == false && answerTwoCorrect == false;

if (bothAnswersIncorrect == true) {
    Console.WriteLine("Both questions were wrong!");
}
else {
    if (answerOneCorrect == true && answerTwoCorrect == true) {
        Console.WriteLine("Both questions were right!");
    }
    else {
        if (answerOneCorrect == false) {
            Console.WriteLine("question one was wrong!");
        }
        
        if (answerTwoCorrect == false) {
            Console.WriteLine("question two was wrong!");
        }
    }
}
Copyright Mikiel Agutu 2019