集册 Java实例教程 打破循环

打破循环

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

584
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
打破循环

class BreakDemo {

    public static void main(String[] args) {

    /*
    时   代     Java  公  众  号 - nowjava.com 提 供
    */

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

        int searchfor = 12;


        int i;

        boolean foundIt = false;


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

            if (arrayOfInts[i] == searchfor) {

                foundIt = true;

                break;

            }//from 时 代 J a v a 公 众 号 - N o w J a v  a . c o m

        }


        if (foundIt) {

            System.out.println("Found " + searchfor + " at index " + i);

        } else {

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

        }

    }

}


展开阅读全文