Java break 语句可以直接强行退出当前的循环,忽略循环体中任何其他语句和循环条件测试。以下实例使用了 break 关键字来跳出当前循环:
public class Main {
public static void main(String[] args) {
int[] intary = { 99,12,22,34,45,67,5678,8990 };
int no = 5678;
int i = 0;
boolean found = false;
for ( ; i < intary.length; i++) {
if (intary[i] == no) {
found = true;
break;
}
}
if (found) {
System.out.println(no + " 元素的索引位置在: " + i);
}
else {
System.out.println(no + " 元素不在数组总");
}
}
}
以上代码运行输出结果为:
5678 元素的索引位置在: 6

本文通过一个具体的示例展示了如何在Java中使用break语句来提前终止循环。示例中,在数组中查找特定元素,一旦找到该元素即使用break退出循环,并输出该元素的索引位置。
209

被折叠的 条评论
为什么被折叠?



