【发布时间】:2011-12-05 21:42:26
【问题描述】:
我正在编写一个类似 C# 的函数:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
string hello = "";
}
}
这基本上计算了数字,如果 i 大于 20,则不应写入 console.writeline。它应该跨过并点击“out1”,但“out1”最终需要有一个函数才能编译。它需要有 "string hello = """ 才能编译。我不需要“字符串你好=”“”。我只是希望它什么都不做并结束循环。有没有办法做到这一点,而不需要 out1: 语句需要的“string hello = """?喜欢:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
}
}
谢谢。
【问题讨论】:
-
反转逻辑,扔掉goto
-
或者不要颠倒逻辑,用break代替goto。
-
为什么不用for语句从0到20?