Java 循环中断在字符串数组的开关块内不起作用

Java 循环中断在字符串数组的开关块内不起作用,java,arrays,for-loop,switch-statement,Java,Arrays,For Loop,Switch Statement,请参考下面的代码,其中环路中断在开关块内不起作用,您能提供帮助吗 String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" }; //For loop is not breaking after getting the correct value for (int t = 0; t < Books.length; t++) { switch (Books[t]

请参考下面的代码,其中环路中断在开关块内不起作用,您能提供帮助吗

    String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };

    //For loop is not breaking after getting the correct value
    for (int t = 0; t < Books.length; t++) {
      switch (Books[t]) {
        case "Harry Potter":
             System.out.println("Getting from switch case " + t + " " + Books[t]);
            break;
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
    }
stringbooks[]={《哈利波特》、《杀死一只知更鸟》、《饥饿游戏》};
//For循环在获得正确值后未中断
for(int t=0;t
好吧,这个中断只会中断switch语句。你可以试着用一个,例如

循环:
for(int t=0;t

或者,您可以将循环放在它自己的方法中,并使用return语句。

好吧,该中断只会中断switch语句

循环:
for(int t=0;t

或者,您可以将循环放在它自己的方法中,并使用return语句。

break
开关中使用时,只中断开关流,而不中断for循环,因此
如果要为
循环中断
,请在找到正确的值时使用
返回
,该值将
中断
循环并从方法返回,如下所示:

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
      switch (Books[t]) {
        case "Harry Potter":
             System.out.println("Getting from switch case " + t + " " + Books[t]);
            return;//use return when CORRECT CONDITION is found
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
    }
private boolean checkBookExists(String book, int t) {
      boolean bookFound = false;
      switch (book) {
        case "Harry Potter":
            bookFound = true;
            System.out.println("Getting from switch case " + t + " " + book);
            break;
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
        return bookFound;
   }
现在,在如下所示的
for
循环中调用
checkBookExists
方法,当找到该书时,
for
break

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
        if(checkBookExists(Books[t], t)) {
            break;
        }
    }
stringbooks[]={《哈利波特》、《杀死一只知更鸟》、《饥饿游戏》};
for(int t=0;t
break
当在
开关中使用时
语句只中断开关流,而不中断for循环
,因此 如果要为
循环中断
,请在找到正确的值时使用
返回
,该值将
中断
循环并从方法返回,如下所示:

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
      switch (Books[t]) {
        case "Harry Potter":
             System.out.println("Getting from switch case " + t + " " + Books[t]);
            return;//use return when CORRECT CONDITION is found
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
    }
private boolean checkBookExists(String book, int t) {
      boolean bookFound = false;
      switch (book) {
        case "Harry Potter":
            bookFound = true;
            System.out.println("Getting from switch case " + t + " " + book);
            break;
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
        return bookFound;
   }
现在,在如下所示的
for
循环中调用
checkBookExists
方法,当找到该书时,
for
break

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
        if(checkBookExists(Books[t], t)) {
            break;
        }
    }
stringbooks[]={《哈利波特》、《杀死一只知更鸟》、《饥饿游戏》};
for(int t=0;t
请格式化您的代码。只要您返回找到的值,使用return的解决方案就会起作用。如果循环返回void的方法在find和not found之间没有区别,您将需要进行额外的检查,例如检查find标志或!=null您只需为开关
not
ode>请格式化您的代码。只要您返回找到的值,使用return的解决方案就可以工作,如果循环返回void,则find和not found之间没有区别,您需要进行额外的检查,例如检查find标志或!=null您只需为
中断
开关
not
>非常感谢。有效:)非常感谢。有效:)感谢您的解决方案!这也是greatI推荐的选项之一,而不是标签,这是一种不好的做法,请看这里:很好的解决方案,如果您发现他的解决方案有用,请接受它作为参考future@javaguy我喜欢这一点“不要使用标签(在行间跳跃)在与结构化编程相反的代码中。”重要提示,您是对的。跳转标签只适用于汇编等低级语言,在低级编程中使用它是必要的。感谢您的解决方案!这也是greatI推荐的选项之一,而不是标签,这是一种不好的做法,请看这里:很好的解决方案,如果您觉得他的解决方案有用,请访问这里将其作为本文件中的参考future@javaguy我喜欢这一点“不要在代码中使用标签(在行之间跳转),这与结构化编程背道而驰。”重要注意事项,您是对的,跳转标签最好只与汇编等低级语言一起使用,在低级编程中使用它是必要的。