在我的代码中,我有一个for循环,它遍历代码方法,直到满足for条件.
反正有没有摆脱这个for循环?
因此,如果我们查看下面的代码,如果我们想要在"15"时打破这个for循环怎么办?
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Outputs:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x …Run Code Online (Sandbox Code Playgroud) 我已经使用Java很长一段时间了,但我的循环教育有点缺乏.我知道如何创建java中存在的每个循环,并打破循环.但是,我最近想过这个:
假设我有两个嵌套循环.我可以只使用一个
break语句来打破两个循环吗?
这是我到目前为止所拥有的.
int points = 0;
int goal = 100;
while (goal <= 100) {
for (int i = 0; i < goal; i++) {
if (points > 50) {
break; // For loop ends, but the while loop does not
}
// I know I could put a 'break' statement here and end
// the while loop, but I want to do it using just
// one 'break' statement.
points += i;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
如果我在循环中循环并且一旦if语句满足我想要打破主循环,我该怎么做呢?
这是我的代码:
for (int d = 0; d < amountOfNeighbors; d++) {
for (int c = 0; c < myArray.size(); c++) {
if (graph.isEdge(listOfNeighbors.get(d), c)) {
if (keyFromValue(c).equals(goalWord)) { // Once this is true I want to break main loop.
System.out.println("We got to GOAL! It is "+ keyFromValue(c));
break; // This breaks the second loop, not the main one.
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有四个foreach循环遍历集合并根据条件执行某些操作.
这是我现在写的代码:
boolean breakFlag = false;
String valueFromObj2 = null;
String valueFromObj4 = null;
for(Object1 object1: objects){
for(Object2 object2: object1){
// I get some value from object2
valueFromObj2 = object2.getSomeValue();
for(Object3 object3 : object2){
for(Object4 object4: object3){
// Finally I get some value from Object4.
valueFromObj4 = object4.getSomeValue();
// Compare with valueFromObj2 to decide either to break all the foreach loop
breakFlag = compareTwoVariable(valueFromObj2, valueFromObj4 );
if(breakFlag){break;}
} // fourth loop ends here
if(breakFlag){break;}
} // third loop ends here …Run Code Online (Sandbox Code Playgroud) 我正在上高中AP计算机科学课.
我决定向goto我们的一个实验室发表声明,只是为了玩,但我得到了这个错误.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "goto", assert expected
restart cannot be resolved to a variable
at Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)
Run Code Online (Sandbox Code Playgroud)
我goto在Stackoverflow上找到了一个问题,以了解如何正确地完成它,并且我完全按照其中一个答案进行了演示.我真的不明白为什么编译器需要一个assert语句(至少这是我想的它),我也不知道如何使用assert.它似乎希望重启部分goto restart;是一个变量,但重启只是一个标签,将程序拉回到第10行,以便用户可以输入有效的int.如果它想重新启动变量,我该怎么做?
import java.util.*;
public class Factorial
{
public static void main(String[] args)
{
int x = 1;
int factValue = 1;
Scanner userInput = new Scanner(System.in);
restart:
System.out.println("Please enter a nonzero, nonnegative value to be factorialized.");
int factInput = userInput.nextInt();
while(factInput<=0) …Run Code Online (Sandbox Code Playgroud) 假设我有这个:
while(a){
while(b){
if(b == 10)
break;
}
}
Run Code Online (Sandbox Code Playgroud)
问题: break语句是否会将我从两个循环中取出或仅从内循环中取出?谢谢.
我最近读到了这个问题,它有一个关于在Java中标记循环的解决方案.
我想知道Python中是否存在这样的循环命名系统.我曾多次遇到过需要for从内for循环中突破外循环的情况.通常,我通过将内部循环放在一个返回(以及其他)布尔值的函数中来解决这个问题,该布尔值用作断开条件.但是标记循环的中断似乎要简单得多,我想尝试一下,如果python中存在这样的功能
有谁知道它是否存在?
我想打破下面格式的while循环,它有一个if语句.如果if语句为true,则while循环也必须中断.任何帮助,将不胜感激.
while(something.hasnext()) {
do something...
if(contains something to process){
do something
break if condition and while loop
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
int x = 100; //Or some other value
while(x > 0) {
for(int i = 5; i > 0; i++) {
x = x-2;
if(x == 0)
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这只会打破for循环.我怎样才能打破for和while循环呢?
干杯!
java ×8
break ×6
for-loop ×4
loops ×4
while-loop ×3
android ×1
crop ×1
foreach ×1
goto ×1
if-statement ×1
image ×1
label ×1
nested-loops ×1
python ×1
refactoring ×1
syntax ×1
whitespace ×1