C#多选尝试并重启

C# Multiple choice attempts and restart

我的多项选择题似乎有点问题。我似乎无法弄清楚如何在 while 循环中进行尝试。例如,我给你两次尝试,假设你答错了第一个问题。然后它会让你再试一次同样的问题。我想我需要使用 while 循环我似乎无法弄清楚如何实现它。另外,如果不是 100,有什么方法可以重新启动程序吗?

string First;
        int score = 0;
        string Second;


        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;


        Console.Write("Where is  the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");

        First = Console.ReadLine();

        switch (First)

        {

            case "B":

                Console.WriteLine("You entered the correct answer!");



                break;

            case "A":

                Console.WriteLine("You entered the wrong answer.");

                break;

            case "C":

                Console.WriteLine("You entered the wrong answer.");

                break;

            case "D":

                Console.WriteLine("You entered the wrong answer.");

                break;

            default:


                Console.WriteLine("You did not enter a correct answer.");

                break;

        }
        if (First == "B")
        {
            score = score + 50;
            Console.WriteLine("Correct!\n" + " score:" + score + "\n");
        }
        else 
        {
            Console.WriteLine("Wrong!\n" +  " score:" + score + "\n");
        }
        Console.Write("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");

        Second = Console.ReadLine();

        switch (Second)

        {

            case "A":

                Console.Write("You entered the correct answer!");



                break;

            case "B":

                Console.WriteLine("You entered the wrong answer.");

                break;

            case "C":

                Console.WriteLine("You entered the wrong answer.");

                break;

            case "D":

                Console.WriteLine("You entered the wrong answer.");

                break;

            default:


                Console.WriteLine("You did not enter a correct answer.");

                break;

        }
        if (Second == "A")
        {
            score = score + 50;
            Console.WriteLine("Correct!\n" + " score:" + score + "\n");
        }
        else
        {
            Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
        }

改变检查正确答案的方法将有助于使您尝试做的事情更加清晰。我建议制作一个 class 来保存问题信息:

class Question
{
    public string Text { get; private set; }
    public string Answer { get; private set; }

    public Question(string text, string answer)
    {
        Text = text;
        Answer = answer;
    }
}

然后您可以创建问题列表,而无需为每个问题创建大型分支结构:

List<Question> questions = new List<Question>();
questions.Add(new Question("Where is the capital of...", "B"));
questions.Add(new Question("Where is Walt Dis...", "A"));

现在要提问,您可以遍历您的列表。添加第二次机会功能也很容易:

foreach (Question question in questions)
{
    bool answeredCorrectly = false;

    for (int i = 0; i < 2; ++i)  // up to 2 chances
    {
        Console.Write(question.Text);
        string answer = Console.ReadLine();
        if (answer == question.Answer)
        {
            Console.WriteLine("You answered correctly!");
            answeredCorrectly = true;
            break;  // Make sure we break out of the for loop so we don't ask a second time
        }
        else
        {
            Console.WriteLine("That's not correct.");
        }
    }

    if (answeredCorrectly)
    {
        // Add 50 points to their score, etc.
    }
}

你可以这样写 while 循环

while (score!=50) {
            // Do stuff
        }

这就是我要写的程序

    static void Main(string[] args) {

        string First; //You should use a char
        int score = 0;
        string Second;


        //TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;


        Console.Write("Where is  the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");



        // Here is the while loop
        // While score is not 50 do stuff
        while (score!=50) {
        First = Console.ReadLine();
            score = checkanswer(First, "B", 50, score);
        }

        Console.Write("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");



        while (score != 100) { Second = Console.ReadLine();
            score = checkanswer(Second, "A", 50, score);
        }
    }
    // I added this little fancy function. It makes your program more structured and a little bit smaller ;)
    static int checkanswer(string userinput, string rightanswer, int winpoints, int score){
        if (userinput==rightanswer) {
            Console.WriteLine("You entered the wrong answer.\n");
            Console.WriteLine("Correct!\n" + " score:" + winpoints + score + "\n");
            return winpoints + score;
        } else {
            Console.WriteLine("You entered the wrong answer.\n");
            Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
            return score;
        }
    }
bool Ask(string question, string answer, int attempts)
{
    Console.WriteLine(question);
    for (int i = 0; i < attempts; i++)
    {
        string input = Console.ReadLine().ToLower();
        if (input == answer)
        {
            Console.WriteLine("Correct!");
            return true;
        }

         Console.WriteLine("Incorrect.");
    }

     return false;
}

static void Main()
{
    int score = 0;
    if (Ask("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa", "B", 3))
    {
        score += 50;
    }
    else
    {
        score -= 50;
    }
    Console.WriteLine("Score :" + score);
}

正如其他人所说,您应该更改代码的方法,使其更加灵活并避免不必要的重复。但是,如果我要遵循您的结构,您可以使用一个整数变量来存储剩余的尝试次数,并在每次提交错误答案时减少它。将每个问题的代码封装在一个while循环中,条件是剩余的尝试次数> 0。示例:

string First;
int score = 0;
int attempts = 2; //our integer variable 
string Second;
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

while (attempts > 0)
{
    //First question
    Console.WriteLine("Where is  the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa"); //use WriteLine instead of Write, for console clarity
    First = Console.ReadLine();
    switch (First.ToUpper()) //accept lowercase inputs
    {
        case "B": Console.WriteLine("You entered the correct answer!"); break;
        case "A": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break; //same logic
        default: Console.WriteLine("You did not enter a correct answer."); break;
    }
    if (First.ToUpper() == "B")
    {
        score = score + 50;
        Console.WriteLine("Correct!\n" + " score:" + score + "\n");
        break; //force exit loop, onto the next question
    }
    else
    {
        Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
        attempts--; //decrease number of attempts left
    }
 }
 while(attempts > 0)
 {
    //Second question
    Console.WriteLine("Where is Walt Disney World Park located in Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");
    Second = Console.ReadLine();

    switch (Second.ToUpper()) //accept lowercase inputs
    {
        case "A": Console.Write("You entered the correct answer!"); break;
        case "B": case "C": case "D": Console.WriteLine("You entered the wrong answer."); break;
        default: Console.WriteLine("You did not enter a correct answer."); break;
    }
    if (Second.ToUpper() == "A")
    {
        score = score + 50;
        Console.WriteLine("Correct!\n" + " score:" + score + "\n");
        break; //you win!
    }
    else
    {
        Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
        attempts--; //decrease number of attempts left
    }
}

@itsme86 的回答很好,如果你想做面向对象的等等。 但是,如果您只是想学习 while 循环,那么以下是一种非常 simple/stupid/ugly 的方法来实现您正在尝试做的事情:

Console.Write("Where is  the capital of the state of Florida? A.Orlando,B.Tallahassee, C. Miami, or D. Tampa");
        var first = "";
        bool answeredCorrect = false; //Track B answered.
        int attempts = 0; //Track 2 attempts.
        while (!answeredCorrect && attempts < 2)
        {
            first = Console.ReadLine();
            switch (first)
            {

                case "B":

                    Console.WriteLine("You entered the correct answer!");
                    score = score + 50;
                    Console.WriteLine("Correct!\n" + " score:" + score + "\n");

                    answeredCorrect = true;
                    break; //break and skip while because "answeredCorrect" is now true. 

                case "A":

                    Console.WriteLine("You entered the wrong answer.");
                    attempts++; //Inkrement attempts. (One more try. Or skip if attemps is 2)
                    break;

                //case C,D...

                default:
                    Console.WriteLine("You did not enter a correct answer.");
                    attempts++; //Inkrement attempts. (One more try. Or skip if attemps is 2)
                    break;
            }
        }

        if(first != "B")
        {
            Console.WriteLine("Wrong!\n" + " score:" + score + "\n");
        }

(我们公司有人写这种代码)