C#相当于Java的继续<标签> ;? [英] C# equivalent to Java's continue <label>?
问题描述
应该是简单快捷:我想一个C#等同于Java代码:
原稿:对(串一:富){
为(字符串乙:巴){
如果(b.equals(嗡嗡)){
继续原稿;
}
}
//其它代码来这里...
}
修改的:
OK似乎有没有这样的等价(嘿 - 乔恩斯基特自己说没有,那就解决了;))。因此,办法对我来说(其相当于Java)是:
为(串一:富){
布尔foundBuzz = FALSE;
为(字符串乙:巴){
如果(b.equals(嗡嗡)){
foundBuzz = TRUE;
中断;
}
}
如果(foundBuzz){
继续;
}
//其它代码来这里...
}
我不相信有一个等价的,我害怕。你必须要么使用一个布尔值,或只是转到的外循环内结束。它甚至混乱比它的声音,作为一个标签已被应用到一个声明 - 但我们并不想在这里做任何事情。不过,我认为这样做你想让它是什么:
使用系统;
公共类测试
{
静态无效的主要()
{
的for(int i = 0; I< 5;我++)
{
为(INT J = 0; J< 5; J ++)
{
Console.WriteLine(I = {0} J = {1},I,J );
如果(J == I + 2)
{
转到end_of_loop;
}
}
Console.WriteLine(内循环后);
end_of_loop:{}
}
}
}
我会的强烈的建议表达这种不同的方式,但是。我不认为有很多次,没有编码它更可读的方式。
Should be simple and quick: I want a C# equivalent to the following Java code:
orig: for(String a : foo) {
for (String b : bar) {
if (b.equals("buzz")) {
continue orig;
}
}
// other code comes here...
}
Edit: OK it seems there is no such equivalent (hey - Jon Skeet himself said there isn't, that settles it ;)). So the "solution" for me (in its Java equivalent) is:
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...
}
I don't believe there's an equivalent, I'm afraid. You'll have to either use a boolean, or just "goto" the end of the inside of the outer loop. It's even messier than it sounds, as a label has to be applied to a statement - but we don't want to do anything here. However, I think this does what you want it to:
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: {}
}
}
}
I would strongly recommend a different way of expressing this, however. I can't think that there are many times where there isn't a more readable way of coding it.
这篇关于C#相当于Java的继续<标签> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!