如何改掉这段C#中的goto?>>>>

来源:百度知道 编辑:UC知道 时间:2024/07/03 13:17:47
不要改变功能,怎么样可以把GOTO改掉?

using System;
class A
{
public static void B(params int[] BN)
{
int sum = 0;
foreach (int i in BN)
{
sum += i;
}
Console.WriteLine("sum is :" + sum);
}
static void Main()
{
int[] a = new int[Int32.Parse(Console.ReadLine())];

for (int j = 0; j < a.Length; j++)
{
a[j] = Int32.Parse(Console.ReadLine());
while (a[j] == 0)
{
goto L;

}
}
L:
B(a);
Console.ReadLine();
}
}

while (a[j] == 0)
{
goto L;

}
这不可以吧,如果a【j】满足==0关系,岂不是死循环?你的判断条件应该是如果把?如果是判断“如果a【j】==0就跳出那个for循环”的话,可以用
if(a[j]==0)
break;
即可跳出循环到达下面的语句,也就是你的L,改了以后可以去掉L标号。

果然我觉得不大对劲.
for()
{
___while( a[j]==0 )
___{ goto L;}
}

为什么不写成
for()
{
___if( a[j]==0 )
___{ break;}
}

while (a[j] == 0)
{
goto L;

}
就是这有问题

万恶的goto语句。bug的滋生黑洞