Java 示例 - break 语句的使用
-
问题描述
方法中如何使用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("Found the no: " + no + " at index: " + i); } else { System.out.println(no + "not found in the array"); } } }
-
结果
上面的代码示例将产生以下结果。Found the no: 5678 at index: 6