应该简单快捷:我想要一个与以下Java代码等效的C#:
orig: for(String a : foo) {
for (String b : bar) {
if (b.equals("buzz")) {
continue orig;
}
}
// other code comes here...
}
Run Code Online (Sandbox Code Playgroud)
编辑:好吧,似乎没有这样的等价物(嘿 - Jon Skeet自己说没有,这就解决了;)).所以我的"解决方案"(在其Java等价物中)是:
for(String a : foo) {
bool foundBuzz = false;
for (String b : bar) {
if (b.equals("buzz")) {
foundBuzz = true;
break;
}
}
if (foundBuzz) {
continue;
}
// other code comes here...
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 32
我不相信有一个等价的东西.你必须要么使用布尔值,要么只是"转到"外部循环内部的末端.它甚至比听起来更麻烦,因为标签必须应用于声明 - 但我们不想在这里做任何事情.但是,我认为这可以满足您的需求:
using System;
public class Test
{
static void Main()
{
for (int i=0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.WriteLine("i={0} j={1}", i, j);
if (j == i + 2)
{
goto end_of_loop;
}
}
Console.WriteLine("After inner loop");
end_of_loop: {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
不过,我强烈建议采用不同的表达方式.我不能认为有很多次没有更可读的编码方式.
小智 10
其他可能性是使用内循环来实现一个功能:
void mainFunc(string[] foo, string[] bar)
{
foreach (string a in foo)
if (hasBuzz(bar))
continue;
// other code comes here...
}
bool hasBuzz(string[] bar)
{
foreach (string b in bar)
if (b.equals("buzz"))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14139 次 |
| 最近记录: |