C#多选尝试并重启

时间:2017-03-06 16:02:40

标签: c# while-loop 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");
        }

5 个答案:

答案 0 :(得分:1)

改变你检查正确答案的方法将有助于使你想要做的更清楚。我建议上课来提问问题:

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.
    }
}

答案 1 :(得分:1)

你可以像这样编写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;
        }
    }

答案 2 :(得分:0)

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);
}

答案 3 :(得分:0)

正如其他人所说,您应该更改代码的方法,以使其更灵活,避免不必要的重复。但是,如果我要遵循您的结构,您可以使用整数变量来存储剩余的尝试量,并在每次提交错误答案时减少它。在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
    }
}

答案 4 :(得分:0)

@ itsme86的答案很好,如果你想做面向对象等等。 但是如果你只是想学习while循环,那么以下是一种非常简单/愚蠢/丑陋的方式来实现你想要做的事情:

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");
        }

(我们公司的一些人写这种代码)