6.0b: Challenge Answers

Bronze:

class Program
{
    static void Main(string[] args)
    {
        int value = 100 / 2;
        if (value == 50)
        {
            Console.WriteLine(true);
        }

        else
        {
            Console.WriteLine(false);
        }
    }
}

Silver:

class Program
{
    static void Main(string[] args)
    {
        double value = 101d / 2d;
        if (value == 50d)
        {
            Console.WriteLine(true);
        }

        else if (value != 50d)
        {
            Console.WriteLine(false);
        }
    }
}

Gold:

Your programs may vary a little bit but it should look something like this: 1. Print the rules of the game to the console 2. Save the user's input to the ReadLine() and use Convert.ToInt16() so the program will convert the user's input back to a string 3. Create if, else if, and else statements inputting the range the statement will use and the data the program will spit out if the statement is true. 4. Try typing if then press the tab key twice and it will scaffold out an if statement for you.

Discussion

if statements will always be run within the program. Else if statements will only be run if the previous statements were not true. Else statements will only be run if none of the other statements were true.

 class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Can you guess what number I am thinking of? It is between 1-20");
        int guessNumber = Convert.ToInt16(Console.ReadLine());
        if (guessNumber <= 7 && guessNumber >= 1)
        {
            Console.WriteLine("You need to go much higher!");
        }
        else if (guessNumber <= 14 && guessNumber > 7)
        {
            Console.WriteLine("Little bit higher!");
        }
        else if (guessNumber == 15)
        {
            Console.WriteLine("Winner Winner Chicken Dinner!");
        }
        else if (guessNumber >= 16 && guessNumber <= 20)
        {
            Console.WriteLine("Oops, little bit lower");
        }
        else
        {
            Console.WriteLine("Did you read the instructions?");
        }
        Console.ReadLine();
    }
}

Next: Conditionals

Last updated