Java使用break语句?

在Java中,如何使用使用break语句?

下面示例中,演示如何使用break语句跳出循环(实现查找数值:1122,找到后退出循环)

package com.zaixian;

public class UseOfBreakStatement {
    public static void main(String[] args) {
        int[] intary = { 199, 212, 252, 34, 5, 1122, 67, 5678, 8990 };
        int no = 1122;
        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: 1122 at  index: 5

上一篇: Java方法 下一篇: Java文件