如何计算c#(goto循环)中的循环数
我已经创建了一个测试程序,其中包括5个问题。如果你答错了一个问题,你将被迫从头开始,我通过 将代码如何计算c#(goto循环)中的循环数,c#,.net,asp.net-mvc,entity,C#,.net,Asp.net Mvc,Entity,我已经创建了一个测试程序,其中包括5个问题。如果你答错了一个问题,你将被迫从头开始,我通过 将代码转到开始位置并将start:放置在我希望循环的位置 我的问题是:我想在结尾添加一个部分,上面写着程序循环了多少次,这样我就可以添加一条写线,上面写着“干得好,你花了X次才完成这个测试” 下面是程序的主要部分:(不带名称空间和using语句) /*--------------------------------------------------------------声明--------------
转到开始位置代码>并将start:
放置在我希望循环的位置
我的问题是:我想在结尾添加一个部分,上面写着程序循环了多少次,这样我就可以添加一条写线,上面写着“干得好,你花了X次才完成这个测试”
下面是程序的主要部分:(不带名称空间和using语句)
/*--------------------------------------------------------------声明------------------------------------------*/
字符串q1、q2、q3、q4、q5;
/*----------------------------------------标题------------------------------------------*/
WriteLine(“欢迎参加终极测试!”);
Console.WriteLine();
/*----------------------------------------问题1------------------------------------------*/
开始:
WriteLine(“JavaScript语言不是面向对象的(True/False)”;
Console.WriteLine();
q1=Console.ReadLine();
q1=q1.ToUpper();
如果(q1=“真”)
{
Console.WriteLine();
Console.WriteLine(“做得好,您可以继续下一个问题”);
}
其他的
{
WriteLine(“对不起,你的答案错了,你必须重新开始”);
转到开始;
}
Console.WriteLine();
/*----------------------------------------问题2------------------------------------------*/
Console.WriteLine(“在英国获得学徒资格的年龄范围是多少?请输入以下格式xx yy”);
Console.WriteLine();
q2=Console.ReadLine();
如果(q2==“16-24”)
{
Console.WriteLine();
Console.WriteLine(“做得好,您可以继续下一个问题”);
}
其他的
{
WriteLine(“对不起,你的答案错了,你必须重新开始”);
转到开始;
}
Console.WriteLine();
/*----------------------------------------问题3------------------------------------------*/
WriteLine(“HTML是一种编程语言(是或否)”);
Console.WriteLine();
q3=控制台.ReadLine();
q3=q3.ToUpper();
如果(第三季度=“否”)
{
Console.WriteLine();
Console.WriteLine(“做得好,您可以继续下一个问题”);
}
其他的
{
WriteLine(“对不起,你的答案错了,你必须重新开始”);
转到开始;
}
Console.WriteLine();
/*----------------------------------------问题4------------------------------------------*/
WriteLine(“在JavaScript中,用来表示单行注释的两个字符是什么?”);
Console.WriteLine();
q4=Console.ReadLine();
如果(q4==“/”)
{
Console.WriteLine();
Console.WriteLine(“做得好,您可以继续下一个问题”);
}
其他的
{
WriteLine(“对不起,你的答案错了,你必须重新开始”);
转到开始;
}
Console.WriteLine();
/*----------------------------------------问题5------------------------------------------*/
控制台写入线(“500<600和700<600”);
Console.WriteLine();
Console.WriteLine(“上述陈述是正确的还是错误的?”);
Console.WriteLine();
q5=Console.ReadLine();
q5=q5.ToUpper();
如果(q5==“假”)
{
Console.WriteLine();
Console.WriteLine(“做得好,您可以继续下一个问题”);
Console.WriteLine();
WriteLine(“恭喜你通过了测试!”);
}
其他的
{
WriteLine(“对不起,你的答案错了,你必须重新开始”);
转到开始;
}
Console.WriteLine();
}
}
}
谢谢你的帮助。
int count = 0;
在乞讨时,每当有人失败时,将其发送到重复:
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!";
}
申报
int count = 0;
在乞讨时,每当有人失败时,将其发送到重复:
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!";
}
放
在循环中,你想知道循环的次数,例如,如果你想检查循环是否已经过了15次,那么
if(count = 15)
然后添加您的代码
在循环中,你想知道循环的次数,例如,如果你想检查循环是否已经过了15次,那么
if(count = 15)
然后添加代码只需在代码中添加三行-->
启动前:-->int count=0代码>
启动后:-->count++代码>
代码的最后一行。-->
WriteLine(“做得好,你花了“+count+”次完成了测试”)
只需在代码中添加三行-->
启动前:-->int count=0代码>
启动后:-->count++代码>
代码的最后一行。-->
WriteLine(“做得好,你花了“+count+”次完成了测试”)
好的,你可能已经有了答案,这也是一个解决方案,但是(我希望你不要把这看作是一件消极的事情)我重新制作了整个东西,对我来说,至少它更简单,也更少消耗行数(至少我这么认为)
stringq;
int-retryCount=1;
WriteLine(“欢迎参加终极测试!”);
Console.WriteLine();
开始:
常数int numberOfQuestions=5;
对于(inti=1;i1){Console.WriteLine();}
开关(一){
案例
/*------------------------------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);