集册 Java实例教程 标签突破嵌套for循环

标签突破嵌套for循环

欢马劈雪     最近更新时间:2020-01-02 10:19:05

588
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
标签突破嵌套for循环
//来自 n o w j a v a . c o m - 时  代  Java

class BreakWithLabelDemo {

    public static void main(String[] args) {


        int[][] arrayOfInts = { { 32, 87, 3, 589 }, { 12, 1076, 2000, 8 },

                { 622, 127, 77, 955 } };

        int searchfor = 12;


        int i;

        int j = 0;

        boolean foundIt = false;


        search: for (i = 0; i < arrayOfInts.length; i++) {

            for (j = 0; j < arrayOfInts[i].length; j++) {

                if (arrayOfInts[i][j] == searchfor) {

                    foundIt = true;

                    break search;

                }

            }

        }


        if (foundIt) {

            System.out//n o w    j a v a  . c o m 提供

                    .println("Found " + searchfor + " at " + i + ", " + j);

        } else {

            System.out.println(searchfor + " not in the array");

        }

    }

}


展开阅读全文