Adrian Simon
Adrian Simon

Reputation: 21

Why is the if statement on Visual C# not being evaluated properly?

I'm trying to make a Times Table Console Application in Visual Studio 2015 Community using Visual C#, but the if statement isn't being evaluated properly. I started to debug (test) my console application, but the if statement wasn't evaluated properly in the last three questions. Can someone help? Here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace xTables
    {
        class Program
        {
            static void Main(string[] args)
            {
                  Console.WriteLine("Welcome to xTables");
        Console.WriteLine("In this application, you'll have to answer questions from");
        Console.WriteLine("the times table up to 12");
        Console.WriteLine("Good Luck!");
        //Question 1
        Console.WriteLine("What is 1 x 6");
        string userAnswer = Console.ReadLine();


        if (userAnswer == "6")
        {
            string message = "Correct";
            Console.WriteLine(message);
        }

        else
        {
            string message = "Incorrect";
            Console.WriteLine(message);
        }



        //Question 2
        Console.WriteLine("What is 2 x 3");
        Console.ReadLine();

        if (userAnswer == "6")
        {
            string message = "Correct";
            Console.WriteLine(message);
        }

        else
        {
            string message = "Incorrect";
            Console.WriteLine(message);
        }



        //Question 3
        Console.WriteLine("What is 8 x 9");
        Console.ReadLine();

        if (userAnswer == "72")
        {
            string message = "Correct";
            Console.WriteLine(message);
        }

        else
        {
            string message = "Incorrect";
            Console.WriteLine(message);
        }


        //Question 4
        Console.WriteLine("What is 5 x 6");
        Console.ReadLine();

        if (userAnswer == "30")
        {
            string message = "Correct";
            Console.WriteLine(message);
        }

        else
        {
            string message = "Incorrect";
            Console.WriteLine(message);
        }

        //Question 5
        Console.WriteLine("What is 4 x 6");
        Console.ReadLine();

        if (userAnswer == "24")
        {
            string message = "Correct";
            Console.WriteLine(message);
        }

        else
        {
            string message = "Incorrect";
            Console.WriteLine(message);
        }


    }
}

}

Upvotes: 0

Views: 87

Answers (6)

user3532232
user3532232

Reputation: 257

You're not returning the readline() into a varible. Guessing you want to do: userAnswer = Console.ReadLine();

Upvotes: 0

itsme86
itsme86

Reputation: 19496

You need to use the return value of Console.ReadLine().

userAnswer = Console.ReadLine();

As a side note, this is an excellent candidate for a method:

HandleQuestion(1, 6);
HandleQuestion(2, 3);
HandleQuestion(8, 9);
HandleQuestion(5, 6);
HandleQuestion(4, 6);

void HandleQuestion(int operand1, int operand2)
{
    Console.WriteLine("What is {0} x {1}", operand1, operand2);
    string userAnswer = Console.ReadLine();

    if (userAnswer == (operand1 * operand2).ToString())
        Console.WriteLine("Correct");
    else
        Console.WriteLine("Incorrect");

}

Once you've done that, you can move into generating questions with random values for the operands.

Upvotes: 0

tCoe
tCoe

Reputation: 401

besides the above mentioned answers you really don't need to use the abstraction method for the message variable.

 Console.WriteLine("Correct") 

would work just as well

Upvotes: 0

I think you should use the following:

if (userAnswer.Equals(6)){
 // Rest of the code

 }

Upvotes: -1

user94559
user94559

Reputation: 60143

In all but the first question, you have this:

Console.ReadLine();

When you probably want this:

userAnswer = Console.ReadLine();

EDIT for further explanation

Because the answer to the second question is the same as the answer to the first, it may appear to be working. (You may be typing the same answer twice.) But in reality, it's not just the last three questions that aren't working; only the first question is actually doing what you want.

Upvotes: 3

Matt Coats
Matt Coats

Reputation: 332

In the last three you need to do:

userAnswer = Console.ReadLine();

Instead of just Console.ReadLine();

Upvotes: 1

Related Questions