Moose9000
Moose9000

Reputation: 11

If statement multiple choice c#

string question1;

//Question 1
WriteLine("What is his favourite sport? ");
WriteLine("a) Soccer");
WriteLine("b) Basketball ");
WriteLine("c) Tennis ");
WriteLine("d) Football " );
Write("your answer is: ");
question1 = ReadLine();

if (question1 != "a" || question1 != "b" || question1 != "c")
    Write("That is incorrect try again");
else
    Write("that is correct");

No matter what letter I put it always gives me an incorrect try again, what am I doing wrong? Also would a while loop be better when trying to create a quiz game?

Upvotes: 1

Views: 2022

Answers (4)

paxdiablo
paxdiablo

Reputation: 881253

Those "or" (||) operators should almost certainly be "and" (&&) operators.

Think of what happens when you enter c, for example. Obviously, that will be c but it won't be a or b, so two of the sub-conditions will be true.

In fact, that's the case no matter which of a, b, or c that you enter (if you enter something else, all three will be true). And since an x || y || z is true if any of its sub-components is true, the expression as a whole is always true. The following table hopefully illustrates this:

Input != "a" (A) != "b" (B) != "c" (C) A or B or C
a False True True True
b True False True True
c True True False True
d True True True True
other True True True True

In other words, unless question1 is some sort of "Schrodinger's variable" that can be all of a, b, and c at the same time, that if statement of yours will never be false.


However, if the subject's favourite sport is football, I'm not sure why you wouldn't just use the much simpler condition below:

if (question1 == "d") {
    WriteLine("That is correct");
} else {
    WriteLine("That is incorrect try again");
}

That approach (accepting the right answer) seems far easier than what your original code seems to be doing (rejecting all of the the wrong answers).


And, yes, a while loop probably would be better for a quiz scenario, something like the following would be a good start.

I've even added a rudimentary scoring system, because I get bored easily :-)

The points for a question reduce if you answer wrongly: two points for a correct answer first time, one point if you get it right second time, no points otherwise:

int score = 0; // Done once before ALL questions.
int awardPoints;
string answer;

awardPoints = 2; // Done before EACH question.
do {
    // Output the question and get an answer.

    WriteLine();
    WriteLine("What is his favourite sport (x to exit)? ");
    WriteLine("  a) Soccer");
    WriteLine("  b) Basketball");
    WriteLine("  c) Tennis");
    WriteLine("  d) Football" );
    Write("Your answer: ");
    answer = ReadLine();

    // Evaluate answer (d is correct here). An x answer
    // will produce no output as it assumes you want to
    // just exit.

    if (answer == "d") {
        score += awardPoints;
        WriteLine("Correct, you earned {} points, now at {}",
            awardPoints, score);
    } else if (answer != "x") {
        // Incorrect answer. Halve question points so
        // user cannot simply keep trying without
        // some penalty.

        WriteLine("Incorrect, try again");
        awardPoints = awardPoints / 2;
    }
} while (answer != 'd' && answer != 'x');

// Here, either the correct answer or 'x' was
// given. If 'x', probably want to stop asking
// questions. Otherwise go on to next question.

Upvotes: 3

Ramesh
Ramesh

Reputation: 13266

The runtime evaluates from left to right. In case of || it stops at the first true and returns true. If there is nothing which is true then it returns false.

In case of && it stop at the first false and returns false.

The readable code if to check for right answer and say correct and for all others display incorrect. This will also take care of user entering any other value other than "a","b,"c" as well like say "t"

Upvotes: 0

Suyash Gaur
Suyash Gaur

Reputation: 2861

When you use || operator, as soon as any condition is true, it stops processing rest of the cases and marks the statement as true.

The best way is, to test the correct answer and put rest of the cases in else block
and mark them as 'incorrect'

if (question1 == 'correctOption')
{
System.Console.Write("That is correct.");  
}
else
{
System.Console.Write("That is incorrect try again");  
}

Upvotes: 0

Thomson Mixab
Thomson Mixab

Reputation: 657

Please, try this way.

void Main()
{
    string question1 = "";

    do
    {
        //Question 1
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("What is his favourite sport? ");
        Console.WriteLine("a) Soccer");
        Console.WriteLine("b) Basketball ");
        Console.WriteLine("c) Tennis ");
        Console.WriteLine("d) Football ");
        Console.Write("your answer is: ");
        question1 = Console.ReadLine();
        Console.WriteLine(question1);

        if (question1 == "a" || question1 == "b" || question1 == "c")
        {
            Console.Write("That is incorrect try again");
        }
        else
        {
            Console.Write("that is correct");

        }
    } while (question1 != "q");
}

Upvotes: 2

Related Questions