简体   繁体   English

如何使程序流控制跳回到Java中以前的循环?

[英]How to Make Program flow control jump back to a former loop in java?

So I have written a code that allows a user to find a word in a TextArea . 所以我写了一个代码,允许用户在TextArea找到一个单词。 I have nearly succeeded but for one thing. 我几乎成功了,但有一方面。 here, I will show you all the code and tell my problem. 在这里,我将向您展示所有代码并告诉我问题。

if(ta.getText().length() != 0 && t1.getText().length() != 0)
{
    char c1[] = ta.getText().trim().toCharArray();
    char c2[] = t1.getText().trim().toCharArray();
    for(int i=startFlag;i<c1.length;i++)
    {
        if(c1[i]==c2[0])
        {
            start = i;
            break;
        }
    }

    k=start;
    for(int i=0;i<c2.length;i++)
    {
        if(c2[i] != c1[start++])
        {

        }
        else 
            countFlag++;
    }

    if(countFlag==c2.length)
    {
        ta.select(k,c2.length);
        startFlag++;
    }
}

For reference, ta is the TextArea and t1 is the TextField where the user enters a word to find. 作为参考, taTextAreat1TextField ,用户在其中输入要查找的单词。 i have a problem in the second for loop. 我在第二个for循环中遇到问题。 What should I write in the if () block there so that whenever c2[i] != c1[start++] the control is shifted to the first for loop, that would again determine the value of start? 我应该在if ()块中写些什么,以便每当c2[i] != c1[start++] ,控件移至第一个for循环,这将再次确定start的值?

Create a method to get "start" that you can then call whenever you want. 创建一种获取“开始”的方法,然后您可以随时调用它。

if(ta.getText().length() != 0 && t1.getText().length() != 0)
        {
            char c1[] = ta.getText().trim().toCharArray();
            char c2[] = t1.getText().trim().toCharArray();

            k=getStart(startFlag, c1.length);

            for(int i=0;i<c2.length;i++)
            {
                if(c2[i] != c1[start++])
                {
                    start = getStart(startFlag, c1.length);
                }
                else 
                    countFlag++;
            }

            if(countFlag==c2.length)
            {
                ta.select(k,c2.length);
                startFlag++;
            }

        }

And getStart() is: 而getStart()是:

public int getStart(int startFlag, int length) {
    for(int i=startFlag;i<length;i++)
            {
                if(c1[i]==c2[0])
                {
                    return i;
                }
            }
}

You may need different inputs to getStart(), but hopefully this gets across the general idea. 您可能需要不同的输入来获取getStart(),但希望这能贯穿整个思路。

The way your code is set up right now, what you're asking for is impossible. 现在设置代码的方式是不可能的。 To do what you're asking, you'll need to refactor your current code into different methods. 要执行您的要求,您需要将当前代码重构为不同的方法。 More specifically, you'll need to put the for loops into their own methods and then call these methods. 更具体地说,您需要将for循环放入其自己的方法中,然后调用这些方法。

So what you would need to do is make a separate method for the for loop. 因此,您需要为for循环创建一个单独的方法。

public static int startForLoop(int i) {
    for(int i=startFlag;i<c1.length;i++)
    {
       if(c1[i]==c2[0])
       {
          start = i;
          break;
       }
    }
}

Then you can just call startForLoop(0) initially and in the 2nd for loops if statment: 然后,您可以只在开始时调用startForLoop(0)然后在第二个for循环中调用startForLoop(0)

if(c2[i] != c1[start++])
{
    startForLoop(start+1)
}

This will continue the for loop where it left off. 这将从中断的地方继续for循环。 If you need to run the 2nd for loop again then you have to make a separate method for it as well and basically place both of them in a while loop that continues till you find the result you want in the 2nd for loop. 如果您需要再次运行2nd for循环,则还必须为其创建一个单独的方法,并将它们基本上都放置在while循环中,直到继续在2nd for循环中找到所需的结果为止。

May be this code piece help you what you are looking for. 也许这段代码可以帮助您寻找什么。 Basically it moves along with the string to be searched in keeping in mind the index of the string to be searched for. 基本上,它会与要搜索的字符串一起移动,同时要记住要搜索的字符串的索引。

Sorry but i have implemented it in java, but the notion is same and the result returned is the best what i got.you must give it a try! 抱歉,但是我已经用Java实现了它,但是概念是相同的,返回的结果是我得到的最好的结果。您必须尝试一下!

private static String searchString(String searchIN,String searchFOR){
        if (searchFOR != "") {
            int index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase());
            String before = "";
            String highlighted = "";
            String after = "";
            while (index >= 0) {
                int len = searchFOR.length();
                before = searchIN.substring(0, index);
                highlighted = "\"" + searchFOR + "\"";//do what ever you want to do with searched string
                after = searchIN.substring(index + len);
                searchIN = before + highlighted + after;
                index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase(), index + highlighted.length());
            }
        }
        return searchIN;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM