繁体   English   中英

如何让我的程序在使用 do-while 循环时只计算一个单独的单词?

[英]How can I get my program to just count a word that is by itself while using a do-while loop?

我正在尝试制作一个程序来找到一个“亵渎”词(在这种情况下是“猫”)并说出它在我的输入中出现的次数,并且它可以工作,只是它会在它出现时计算那个亵渎词嵌入在另一个词中,如“conCATination”。 为什么会这样? 当这个词单独存在时,我怎样才能让它只计算它? 编辑:我忘了提到它必须使用 if-else 语句以及使用循环来完成这里是到目前为止我的程序:

    private static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        String sentence = "";
        String word1 = "cat";
        int indexofSW1 = 0;
        int currentlocation = 0;
        int sentenceLength = 0;
        int word1Count = 0;
        int word1Length;
        System.out.println("Please enter a sentence.");
        sentence = keyboard.nextLine().toLowerCase().trim();
        word1Length = word1.length();
        indexofSW1 = sentence.indexOf(word1 , indexofSW1);
        sentenceLength = sentence.length();
        char character1;
if (indexofSW1 >= 0)
            do { if (sentenceLength > indexofSW1 + word1Length) 
            {
                character1 = sentence.charAt(indexofSW1 + word1Length);
                switch (character1) {
                case ' ':
                case '\'':
                case '\t':
                case '\"':
                case '.':
                case '?':
                case '<':
                case '>':
                case ',':
                case '[':
                case ']':
                case '{':
                case '}':
                case '(':
                case ')':
                case '-':
                case '_':
                case '\\':
                case '|':
                case '+':
                case '=':
                case '/':
                case '*':
                case '&':
                case '^':
                case '%':
                case '$':
                case '#':
                case '@':
                case '!':

    }
                 currentlocation = sentence.indexOf(word1, currentlocation);
                }

             if (currentlocation == 0 || currentlocation > 0)
             {  
                    currentlocation += word1Length;
                    word1Count++;
            }
}while(currentlocation > -1 && currentlocation > sentenceLength);

 System.out.printf("You have profane word %s %d times in your sentence." , word1 , word1Count);}}

您可能要考虑在此示例中使用正则表达式,因为它似乎是更好的选择:

String source = "cat at the beginning, in the middle cat too, concatenating at the end cat.";
Pattern p = Pattern.compile("(?<=\\b)+(cat)(?=\\b)+");    // "cat" preceded by one or more whitespace characters or word boundary and followed by this too.
Matcher m = p.matcher(source);     // Set matcher to match given pattern
int counter = 0;    // to count occurences of "cat" int given String
while(m.find()) {    // if match was found...
    counter++;    // increment counter
    System.out.println(m.group(1) + " beginning from index: " + m.start(1)); // print match to the console
}
System.out.println("Number of occurences: " + counter);    // print number of occurences of "cat"

您从此代码获得的输出:

cat beginning from index: 0
cat beginning from index: 36
cat beginning from index: 70
Number of occurences: 3

您可以将 pattern.matcher 与正则表达式一起使用

   String cat = "cat concatination cat cat. cat";
        Pattern pattern = Pattern.compile("cat | cat");
        Matcher matcher = pattern.matcher(cat);

        int count = 0;
        while (matcher.find())
            count++;

        System.out.println(count);    // prints 3

暂无
暂无

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

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