0

我正在尝试使用嵌套循环使用另一个用户输入的搜索词数组来搜索用户输入的文本数组,然后输出搜索词以及它们在文本中出现的次数以及总文本的百分比。我认为我走在正确的轨道上,我的问题是每次 if 语句为真时计数器都不会重置。我对编程很陌生——所以我可能完全错了。下面是整个程序。如果有人可以看看并帮我弄清楚我的问题是什么,我将永远感激不尽。

public class termFrequency {
    public static void main(String[] args) {

        String searchTextPeriod, searchTextComma, searchTextApostrophe, searchTextColon, searchTextExclamation, 
        searchTextQuestion, searchText, searchTerm;
        int counter=0, total, searchIndex=0, termIndex=0;
        double percentage=0.0;
        String [] searchArray, termArray;


        searchText = JOptionPane.showInputDialog("Enter a sentence that is at least 20 words long");

        //removes some common punctuation from the searchable text
        searchTextPeriod = searchText.replace(".", "");
        searchTextComma = searchTextPeriod.replace(",", "");
        searchTextApostrophe = searchTextComma.replace("'", " ");
        searchTextColon = searchTextApostrophe.replace(":", " ");
        searchTextExclamation = searchTextColon.replace("!", "");
        searchTextQuestion = searchTextExclamation.replace("?", "");

        searchArray = searchTextQuestion.split(" "); //splits the sentence and and puts it into an array

        total=searchArray.length;
        System.out.println("There are " +total +" words in your sentence");

        searchTerm = JOptionPane.showInputDialog("Enter your search terms here seperated by a space");
        termArray = searchTerm.split(" ");
        DecimalFormat two = new DecimalFormat("#0.00");

        boolean found = false;
        for (termIndex=0; termIndex<termArray.length; termIndex++) 
        {
            for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

                if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
                {
                    counter++; 
                    found = true;

                    percentage= ((double) counter/(double)total) * 100;
                }
                if (found) 
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
                else  
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");

            }
        }
    }
}
4

2 回答 2

0

在第一个循环内移动 found = false。这样,每次迭代都会将其重置为 false。现在,如果它曾经被更改为 true,那么它在整个过程的其余部分都保持真实。

于 2012-07-08T05:16:45.380 回答
0

您必须将“found”上的 if/else 从内部循环移动到第一个循环的末尾。您还需要在第一个循环中重置布尔值和计数器,就像您使用初始值开始分析 termArray 中的每个新单词一样。

   for (termIndex=0; termIndex<termArray.length; termIndex++) 
    {
        counter=0; //Reset the counter for each word in termArray
        found=false; //Reset the "found" flag for each word in termArray
        for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

            if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
            {
                counter++;
                percentage= ((double) counter/(double)total) * 100;

                found=true
                System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
            }
        }
        if (found) 
                System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
        else  
                System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");

    }

顺便说一句,您现在并不需要“找到”变量,如果counter == 0您知道该词尚未在searchArray.

于 2012-07-08T09:47:58.170 回答