怎么打破; ,标签并继续在java中工作。 [英] How does break; , label and continue work in java.

查看:63
本文介绍了怎么打破; ,标签并继续在java中工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程中的菜鸟,我无法弄清楚如何在java中打破,标记和继续工作。请解释它是如何在循环中工作的。





i'm a noob in programming and i cant figure out how does the break, label and continue work in java. please explain how does it work in a loop.


int i;
for(i=1; i<4; i++) {
one: {
two: {
three: {
System.out.println("\ni is " + i);
if(i==1) break one;
if(i==2) break two;
if(i==3) break three;
// this is never reached
System.out.println("won't print");
}
System.out.println("After block three.");
}
System.out.println("After block two.");
}
System.out.println("After block one.");
}
System.out.println("After for.");
}





这是程序。我真的很困惑,这是如何工作的。请向我解释标签和继续之间的区别。



我尝试过的方法:



i确实在程序上尝试了这些但是我并没有完全掌握它。



this is the program . my really confusion is how does this work. And please explain to me the difference between label and continue.

What I have tried:

i did try these on programs but im not totally getting the hang of it.

推荐答案

如果你没有能力谷歌这个,我恭敬地建议软件工程不适合你。 你是什​​么意思我做过尝试和没有完全掌握它? 提出具体问题,我们会尽力提供帮助,但请不要问一个模糊或开放式问题。



更新以回应OP的消息:



A 继续进入它所在的循环的末尾。

A break 退出它所在的循环。

标有 break 退出(转到已标记的块。



i 为1时,您将退出最外层的循环块,因此不会打印行不会被执行。



/ ravi
If you don't have the ability to Google this, I respectfully suggest that software engineering is not for you.  What do you mean by "I did try" and "not totally getting the hang of it"?  Ask specific questions and we'll try to help, but please don't ask a vague or open ended question.

Updated in response to OP's message:

A continue goes to the end of the loop in which it resides.
A break exits the loop in which it resides.
A labelled break exits (goes to the end of) the labelled block.

When i is 1, you're exiting the outermost loop block, so the "won't print" line will not be executed.

/ravi


有一个工具可以让你看看你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java -application.html [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你至。当代码没有达到预期的效果时,你就会接近一个错误。
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于怎么打破; ,标签并继续在java中工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆