如何计算c#中的循环数(goto循环)

本文关键字:循环 何计算 goto 计算 | 更新日期: 2023-09-27 18:21:36

我创建了一个测试程序,其中包含5个问题。如果你答错了一个问题,你就必须从头开始,我通过将代码CCD_ 1和CCD_。

我的问题是:我想在末尾添加一个部分,说明程序循环了多少次,这样我就可以添加一行文字,说"干得好,你花了X次才完成测试"

这是程序的主要部分:(没有名称空间和使用语句)

        /*----------------------------------------Declaration----------------------------------------- */
        string q1, q2, q3, q4, q5;
        /*----------------------------------------TITLE----------------------------------------- */
        Console.WriteLine("Welcome to the Ultimate quiz!");
        Console.WriteLine();
        /*----------------------------------------QUESTION 1----------------------------------------- */
        start:
        Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
        Console.WriteLine();
        q1 = Console.ReadLine();
        q1 = q1.ToUpper();
        if (q1 == "TRUE")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
        }
        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }
        Console.WriteLine();
        /*----------------------------------------QUESTION 2----------------------------------------- */
        Console.WriteLine("What is the age range to qualify for an apprenticeship in the uk? Please type in the following format xx-yy");
        Console.WriteLine();
        q2 = Console.ReadLine();
        if (q2 == "16-24")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
        }
        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }
        Console.WriteLine();
        /*----------------------------------------QUESTION 3----------------------------------------- */
        Console.WriteLine("Is HTML a programming language (Yes or No)");
        Console.WriteLine();
        q3 = Console.ReadLine();
        q3 = q3.ToUpper();
        if (q3 == "NO")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
        }
        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }
        Console.WriteLine();
        /*----------------------------------------QUESTION 4----------------------------------------- */
        Console.WriteLine("In JavaScript, What are the 2 charecters used to symbolise a single line comment?");
        Console.WriteLine();
        q4 = Console.ReadLine();
        if (q4 == "//")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
        }
        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }
        Console.WriteLine();
        /*----------------------------------------QUESTION 5----------------------------------------- */
        Console.WriteLine("500 < 600 && 700 < 600");
        Console.WriteLine();
        Console.WriteLine("Is the above statement true or false ?");
        Console.WriteLine();
        q5 = Console.ReadLine();
        q5 = q5.ToUpper();
        if (q5 == "FALSE")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            Console.WriteLine();
            Console.WriteLine("Congratulations You have passed the quiz!");
        }
        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }
        Console.WriteLine();

        }
    }
}

谢谢你的帮助。

如何计算c#中的循环数(goto循环)

只需在代码中添加三行即可-->

  1. 启动前:-->int count = 0;
  2. 启动后:-->count++;
  3. 代码的最后一行。-->

    Console.WriteLine("干得好,你花了"+count+"次完成测验");

好的,所以你可能已经有了答案,这也是一个解决方案,但(我希望你不要把这看作是一件负面的事情)我重新制作了整个事情,至少对我来说它更简单,线路消耗更少(至少我认为是这样)

            string q;
            int retryCount = 1;
            Console.WriteLine("Welcome to the Ultimate quiz!");
            Console.WriteLine();
            start:
            const int numberOfQuestions = 5;
            for (int i = 1; i <= numberOfQuestions; i++) {
                if (i > 1) {Console.WriteLine();}
                switch (i) {
                    case 1:
                        Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
                        break;
                    case 2:
                        Console.WriteLine("What is the age range to qualify for an apprenticeship in the uk? Please type in the following format xx-yy");
                        break;
                    case 3:
                        Console.WriteLine("Is HTML a programming language (Yes or No)");
                        break;
                    case 4:
                        Console.WriteLine("In JavaScript, What are the 2 charecters used to symbolise a single line comment?");
                        break;
                    case 5:
                        Console.WriteLine("500 < 600 && 700 < 600");
                        Console.WriteLine();
                        Console.WriteLine("Is the above statement true or false ?");
                        break;
                }
                Console.WriteLine();
                q = Console.ReadLine();
                q = q.ToUpper();
                switch (i) {
                    case 1:
                        if (q == "TRUE") {
                            Console.WriteLine();
                            Console.WriteLine("Well Done, you may move on to the next question");
                        } else {  goto restart; }
                        break;
                    case 2:
                        if (q == "16-24") {
                            Console.WriteLine();
                            Console.WriteLine("Well Done, you may move on to the next question");
                        } else { goto restart; }
                        break;
                    case 3:
                        if (q == "NO") {
                            Console.WriteLine();
                            Console.WriteLine("Well Done, you may move on to the next question");
                        } else { goto restart; }
                        break;
                    case 4:
                        if (q == "//") {
                            Console.WriteLine();
                            Console.WriteLine("Well Done, you may move on to the next question");
                        } else { goto restart; }
                        break;
                    case 5:
                        if (q == "FALSE") {
                            Console.WriteLine();
                            goto end;
                        } else { goto restart; }
                        break;
                }
            }
            restart:
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            retryCount += 1;
            goto start;

            end:
            Console.WriteLine("Congratulations You have passed the quiz!");
            Console.WriteLine(String.Format("Well done!, you took {0} times to complete the quiz.", retryCount));
            Console.ReadKey();

我希望这就是你想要实现的,你不需要使用所有的代码,只是表明你不需要创建q1、q2、q3等…

随意添加更多案例即可。

放入

count++

在循环中,你想知道它进行了多少次循环,例如,如果你想检查循环是否进行了15次,那么

if(count = 15)

然后添加您的代码

声明

int count = 0;

在开始时,每次有人失败时都会将其发送到repeat:

repeat:
count++;
goto start;

像这样:

void a() {
                string q, q1;
                int count = 0;
            start:
                int a = 2;
                if (a != 3) goto repeat;
                else goto end;
            repeat:
                count++;
                goto start;
            end:
                string congrats = "you have repeated: " + count.ToString() + "times!";
            }

尽管其他人已经回答了特定的goto问题,但我想提供一种不使用gotos的方法,从长远来看,如果你想在循环时计数,这可能会更好。

添加计数:

    string q1, q2, q3, q4, q5;
    int count = 0;

然后让我们使用while循环。

    /*------------------------------QUESTION 1-------------------------------- */
    while (true)
    {
        ++count;
        Console.WriteLine("The JavaScript Language is not object oriented (True/False)");
        //... other questions as they were....
        // with one small change e.g.
        if (q1 == "TRUE")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
        }
       else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            continue; // <-----------instead of goto start;
        }
        //... other questions as they were....
        //with one small change
       if (q5 == "FALSE")
       {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            Console.WriteLine();
            Console.WriteLine("Congratulations You have passed the quiz!");
            break; //<--------- if they get the final question right,
                   // break out of the while loop
       }

    } //<- end of while loop
    //Output message you suggested
    Console.WriteLine("You took {0} attempts", count);

实际上,goto和while/content/break具有相同的效果,但赋予程序更多的结构。

如果你想给他们一个最大的尝试次数,你可以开始考虑使用for循环。