Contents

Kata 5

Setup instructions

  • Download the starting folder for this Kata.
  • Open a Command Prompt window and navigate to the starting folder using cd.
  • Open the starting folder for this Kata in VSCode.

Overview

In this Kata you’ll look at how to construct logical expressions in C#. You’ll learn how to use if statements to make your program behave differently based on the result of logical expressions. We’ll use the example of a quiz game to introduce these concepts.

Kata instructions

Before you start, there are a few changes to the code compared to what we did in Kata 4.

The code that asks for user’s name has changed. Instead of taking the user’s name as an argument, we now ask for it from the Standard Input. We have also simplified the code that asks for the player’s age.

In Program.cs, alter the prompt for the user’s age to ask for their age in years:

Console.WriteLine("Hello " + name);
Console.WriteLine("Please tell me how old you are in years!");

Rename age variable to ageInYears. This will be used in multiple places in Program.cs, so make sure to change them all.

Now we have the user’s age in years, we can use logic to determine if the input is valid.

For the user’s age in years, it doesn’t make sense for them to input anything less than 0. Now would a newborn baby honestly be using this software? Well it’s not for us to judge… We just need to validate based on possible inputs!

Informally written, the rule might be:

User's age in years must be greater than or equal to 0

To implement that rule in C#, write:

bool isAgeValid = ageInYears >= 0;

Remember, the >= sign means ‘greater than or equal to’

Use if statement to determine if we should continue with the quiz. Write:

if (isAgeValid == true) {

} else {
    
}

Inside the first curly brace block, we’ll write our quiz code. Inside the else block, we’ll write a message telling the user their age is invalid and that they need to start again. Write:

if(isAgeValid == true) {
    Console.WriteLine("Age is valid! Starting quiz!");
} else {
    Console.WriteLine("Age is invalid...Sorry!");
}

Remember to ensure the new code you write is indented correctly. You can use the tab key to indent code like it’s displayed here.

Run it using dotnet run to see if it works.

The quiz

Next let’s write the quiz questions. The code for each question will be like this:

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

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

Add this code to the first if block. It should look something like this:

if(isAgeValid == true) {
    Console.WriteLine("Age is valid! Starting quiz!");

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

    if(answerOne == "1912") {
        Console.WriteLine("Correct!");
    }
    else {
        Console.WriteLine("Incorrect!");
    }
} else {
    Console.WriteLine("Age is invalid...Sorry!");
}

You can add another question of your choice by repeating this if block. Just replace answerOne with answerTwo and so on. The entire thing could look something like this:

if(isAgeValid == true) {
    Console.WriteLine("Age is valid! Starting quiz!");

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

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

    Console.WriteLine("Some other question...");
    string answerTwo = Console.ReadLine();

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

    // And so on for other questions...

} else {
    Console.WriteLine("Age is invalid...Sorry!");
}

Note that I’ve used a comment in this example. Comments in C# start with //, followed by some text. Comments are used to clarify code - they are ignored by the complier and have no effect on how to program works.

Do dotnet run to see if it works.

Winner winner chicken dinner

Finally we’re going to use a bool to track if the user is winning. Above the first if block, write:

bool isWinning = true;

And inside each else block, where the answer is incorrect, write:

isWinning = false;

For example, the code might look like this:

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

Finally, at the end of the block use isWinning to say if the user won or lost. Write:

if (isWinning == true) {
    Console.WriteLine("You won!");
}
else {
    Console.WriteLine("You lost!");
}

Finished!

In this Kata you used logic and conditions to write a simple quiz program.

Try on your own…

Q1

Some questions might have multiple correct answers. For instance, the question:

How many are in a dozen?

Might reasonably be answered 12 or twelve. Using logical operators, amend the code so that it’s possible to have more than one correct answer for a question.

Q2

It would be nicer for the user to see a summary of which questions they got wrong, and which ones they got right at the end of the game.

Amend the code so that:

  • If both questions were wrong, output Both questions were wrong!
  • Otherwise:
    • If question one was wrong, output question one was wrong!
    • If question two was wrong, output question two was wrong!
  • If both questions were correct, output All questions correct! instead of all of the above
Copyright Mikiel Agutu 2019