使用for循环查找带有count的句子中的重复单词 [英] find the duplicate word from a sentence with count using for loop

查看:386
本文介绍了使用for循环查找带有count的句子中的重复单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是java的新手,我有一个任务只能查找重复的单词及其计数。我卡在一个地方,我无法得到适当的输出。我不能使用任何集合和内置工具。我尝试了下面的代码。需要一些帮助,请帮帮我。

As i am new to java i got a task to find duplicate word only and its count. i stuck in a place and i am unable to get the appropriate output. I can not use any collections and built in tool. i tried the below code. Need some help, Please help me out.

public class RepeatedWord 
  {
   public static void main(String[] args) 
      {
          String sen = "hi hello hi good morning hello";
          String word[] = sen.split(" ");
          int count=0;
          for( int i=0;i<word.length;i++)
             {
                for( int j=0;j<word.length;j++)
                   {
                       if(word[i].equals(word[j]))
                          {
                             count++;
                          }
                         if(count>1)
                   System.out.println("the word "+word[i]+" occured"+ count+" time");
                   }

             }

       }
 }

期待输出: -

the word hi occured 2 time
the word hello occured 2 time

但我得到如下输出: -

but i am getting output like below :-

the word hi occured 2 time
the word hi occured 2 time
the word hi occured 2 time
the word hi occured 2 time
the word hello occured 2 time
the word hi occured 2 time
the word hi occured 2 time
the word hi occured 2 time
the word hi occured 2 time
the word hello occured 2 time

请帮助我得到像我期待的输出。请解释一下。所以我也能理解
提前致谢

please help me to get the output like i am expecting. and please explain. so that i can understand too. Thanks in advance

推荐答案

您需要仅为外部循环打印结果。此外,您需要避免检查在上一次迭代中已经检查过的单词:

You need to print the result only for the outer loop. Also, you need to avoid checking the words that were already checked in previous iteration:

for (int i = 0; i < word.length; i++) {
    int count = 0; // reset the counter for each word

    for (int j = 0; j < word.length; j++) {

        if (word[i].equals(word[j])) {
            /* if the words are the same, but j < i, it was already calculated
               and printed earlier, so we can stop checking the current word
               and move on to another one */
            if (j < i) {
                break; // exit the inner loop, continue with the outer one
            }

            count++;
        }
    }

    if (count > 1) {
        System.out.println("the word " + word[i] + " occured " + count + " time");
    }
}

更新

围绕此代码的其他说明: if(j< i){break; }

Additional explanation around this code: if (j < i) { break; }

i 是我们计算重复字数的索引, j 是我们对它的比较。由于我们始终从头开始,我们知道如果单词相等而 j<我,它已在早期的外循环运行中处理过。

i is the index of the word we calculate duplicates for, j is the word we compare it against. Since we start always from beginning, we know that if the words are equal while j < i, it was already processed in earlier run of the outer loop.

在这种情况下,使用 break ,我们中断内循环,流程在外循环中继续。由于我们根本没有更新 count ,因此它仍为零,因此打印结果的条件 if(count> 1)不满足且 println 未执行。

In this case, using break, we interrupt the inner loop and the flow continues in the outer loop. As we didn't update count at all, it is still zero and thus the condition for printing the result if (count > 1) is not satisfied and the println is not executed.

单词hello的示例,使用以下部分中的简单伪代码。

Example for the word "hello", using simple pseudo-code in the following part.

第一次出现:

count = 0
    i = 1, j = 0 --> hello != hi                  --> do nothing
    i = 1, j = 1 --> hello == hello, j is not < i --> count++
    i = 1, j = 2 --> hello != hi                  --> do nothing
    i = 1, j = 3 --> hello != good                --> do nothing
    i = 1, j = 4 --> hello != morning             --> do nothing
    i = 1, j = 5 --> hello == hello, j is not < i --> count++
count > 1        --> print the result

第二次出现:

count = 0
    i = 5, j = 0 --> hello != hi           --> do nothing
    i = 5, j = 1 --> hello == hello, j < i --> break, we have seen this pair earlier
count is not > 1 --> result not printed

希望我没有使这个例子更复杂

Hope I didn't make things more complicated with this example

这篇关于使用for循环查找带有count的句子中的重复单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆