简体   繁体   中英

Creating a method for my quiz in C#

I am trying to create a method in my quiz program that will evaluate a users answers for different questions.

I am currently using a do...while loop with an if...else statement in every question, but I would like to transfer all that into its own method that I can invoke in each question in order to clean everything up.

This is what I currently have:

public static void CurrentQuestion(string userAnswer, string correctAnswer)
{
    Console.ReadLine();
    do
    {
        if (userAnswer == correctAnswer)
        {
            Console.WriteLine("\nThat is correct!");
            break;
        }
        else if (userAnswer != correctAnswer)
        {
            Console.WriteLine("\nSorry, that is incorrect");
            break;
        }
        else
        {
            Console.WriteLine("\nError - Not a Valid Input");
        }
    }
    while ((userAnswer != "A") || (userAnswer != "B") || (userAnswer != "C") || (userAnswer != "D"));
}

And the place where I invoke it:

CurrentQuestion("", "D");

The problem I having now is that when I run the program on the console and I input an answer it always tells me that it is incorrect. I am not sure what it wrong with the code, or what I need to do to get it to work properly.

I would also like to be able to differentiate between lower case and upper case and letters that aren't A, B, C, or D when inputing an answer if possible, but I'm currently just trying to get it to work properly first.

You have several problems here.

First, as several people have pointed out in the comments, you're not storing the results of Console.ReadLine() .

Secondly, your else statement isn't reachable. Think about it: either userAnswer == correctAnswer or it doesn't. What third choice is there?

Third, this is trivially always true:

(userAnswer != "A") || (userAnswer != "B") || (userAnswer != "C") || (userAnswer != "D")

Think about it - if userAnswer == "A" , then obviously userAnswer != "B" . This means that this is an infinite loop.

I think that you meant to use && here. This would mean "keep looping until the user enters either "A", "B", "C", or "D"".

You may also want to check out DeMorgan's laws , which are exceptionally useful for cases like this.

Fourth, there's no way to change userAnswer because you never change it anywhere in code. Even your Console.ReadLine() is outside the loop (and besides, it doesn't do anything anyway because it doesn't store the result).

To simplify this, you could simply use ternary form :

string output = userAnswer == correctAnswer ? "That is correct" : "Sorry, that's incorrect";
Console.WriteLine(output);

if you want to test that the user types A or B or C or D

(userAnswer != "A") || (userAnswer != "B") || (userAnswer != "C") || (userAnswer != "D")

IS wrong. This is always true. If they type A then != B is true and so the whole thing is true (given the or s). You want.

(userAnswer != "A") && (userAnswer != "B") && (userAnswer != "C") && (userAnswer != "D")

This is an example where common human english doesnt match programming english

The other answer by @EJoshuaS is a very good answer. This is another way of 'fixing' your code to achieve the end result.

First, it looks like when calling the method you want to get the user input inside the method. Therefor, making userAnswer a parameter of the method and then passing an empty string when calling the method is redundant. You can get rid of the parameter altogether and simply pass only the correctAnswer .

Next, I assume the intended usage of the while loop is to keep prompting the user to enter either A, B, C, or D in case something else is entered. So I would use do-while(true) , as follows, and if the input is none of A, B, C, or D, prompt the user to enter it again. Once a correct value is entered, evaluate it.

public static void CurrentQuestion(string correctAnswer)
{

    do
    {
        Console.WriteLine("Please enter answer: ");
        string userAnswer = Console.ReadLine();

        if ((userAnswer != "A") && (userAnswer != "B") && (userAnswer != "C") && (userAnswer != "D"))
        {
            Console.WriteLine("\nError - Not a Valid Input");
        }
        else
        {
            if (userAnswer == correctAnswer)
            {
                Console.WriteLine("\nThat is correct!");
                break;
            }
            else if (userAnswer != correctAnswer)
            {
                Console.WriteLine("\nSorry, that is incorrect");
                break;
            }
        }
    } while (true);
}

Explanation: First time, inside the do-while loop, the user is asked to enter the answer. Then we check if it's one of A, B, C, o D. If not, print the Invalid Input message, and do another iteration of the loop. If it is one of the expected four answers, we evaluate it and print either "Correct" or "Incorrect" and exit the loop.

Call it like this:

public static void Main()
{
    string correctAnswer = "D";
    CurrentQuestion(correctAnswer);
    Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM