Public class continue with labeldemo{
   Public static void main (string[]args){
        string searchMe="look for a substring in me";
        string substring="subs";
        booleadn foundIt=false;
        int max=searchMe.length()-substring.length();
        test:
            for(int i=0;i<=max;i++){
            int n=substring.length();
            int i=j;
            int k=0;
            while(n--!=0){
                if(searchMe.charAt(j++)!=substring.charAt(k++)){
                continue test;//跳出FOR循环而不是跳出WHILE循环}
                         }
            foundIt=true;//跳出整个循环
            break test;
         system.out.println(foundIt?"Found it":"Didn't fount it";)
         }
       }
这是实现子串查询的功能,但是我不明白的是那句foundIt=true那句,如果查找未成功的话是怎么跳过去的呢?我对标签不太了解,C++中没有标签吧???

解决方案 »

  1.   

    foundIt=true;
    这句不是控制循环是否继续,它只是保存循环结束后是否找到了。
      

  2.   

    程序写错了吧,应该是int j=i?
    如果没有查找成功,是由continue test直接跳过了fondIt=true一句。只有while做完,即查找成功,才会执行fondIt=true一句,则整个算法完成了。
      

  3.   

    continue test跳过test,那标签的作用范围是到哪儿呢??
      

  4.   

    continue with labeldemo有这样的java语法吗?写错了吧
    continue extends labeldemo
      

  5.   

    如果你只写continue,则continue的作用范围应该是while循环。但你既然明确写了continue test,而test标示的是for语句,自然就作用到for循环了。至于break一句,写不写test都没有关系,都是作用for循环的。
    顺便说一句,你的程序是java程序,但里面错误多多。你没有编译连接运行以下试试吗,编译你都通不过的,不过算法的思路是对的,只要把语法错误改了,就行了。下面是我给你改过的:
    public class test {
    public static void main(String[] arg) {
    String searchMe="look for a substring in me";
    String substring="subss";
    boolean foundIt=false;
    int max=searchMe.length()-substring.length();
    test:
    for(int i=0;i<=max;i++){
    int n=substring.length();
    int j=i;
    int k=0;
    while(n--!=0){
    if(searchMe.charAt(j++)!=substring.charAt(k++))
    continue test;//跳出FOR循环而不是跳出WHILE循环
    }
    foundIt=true;//跳出整个循环
    break;// test;
    }
    System.out.println(foundIt?"Found it":"Didn't fount it");
    }
    }
      

  6.   

    再说一句,这个算法的写法也不是一个好写法,continue test之类的非常不符合结构化流程的规范,建议你不要这么写,还是按照典型的do,while,for循环写好些。
      

  7.   

    下面是我写的算法,给你参考:
    public class test {
    public static void main(String[] arg) {
    String searchMe="look for a substring in me";
    String substring="subs";
    boolean foundIt=false;
    for(int i=0; i<searchMe.length()-substring.length() && !foundIt; i++) {
    boolean all=true;
    for(int j=0; j<substring.length() && all; j++)
    if(searchMe.charAt(i+j)!=substring.charAt(j)) all=false;
    foundIt=all;
    }
    System.out.println(foundIt?"Found it":"Didn't fount it");
    }
    }
      

  8.   

    恩 的确比continue跳转容易理解多了
    看来以前学过的数据结构在这方面可以用上了