使用 for 循环从带计数的句子中找到重复的单词

find the duplicate word from a sentence with count using for loop

因为我是 java 的新手,所以我的任务是仅查找重复的单词及其计数。我卡在一个地方,无法获得适当的输出。我不能使用任何集合和内置工具。我试过下面的代码。需要帮助,请帮帮我。

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

但我得到如下输出:-

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

请帮我得到我期望的输出。并请解释。这样我也能理解。 提前致谢

首先在for循环外打印,这将起到作用并在for循环开始时初始化count=0

 for( int i=0;i<word.length;i++)
             {
                count=0;
                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");
             }

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

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; }

i 是我们计算重复项的词的索引,j 是我们与之比较的词。因为我们总是从头开始,所以我们知道如果单词在 j < i 时相等,它已经在外循环的早期 运行 中处理过。

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

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

第一次出现:

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

希望我没有让这个例子变得更复杂

enter code here
import java.util.*; 
class test{
public static void main(String[] args){
String s;
int count=0;
int count1=0;

System.out.println("Enter the Sentence");
Scanner scan=new Scanner(System.in);
s=scan.nextLine(); 
System.out.println(s);
String[] arr=s.split(" ");

String[] srr=new String[arr.length];
int[] rev=new int[arr.length];
 for(int i=0;i<arr.length; i++)
   { if(arr[i]!="NULL"){
   String temp=arr[i];

 for(int j=i+1;j<arr.length; j++)
 {
 if(temp.equals(arr[j]))
 {
   arr[j]="NULL";
 count++;

 }

   }

  srr[count1]=temp;
 rev[count1]=count;
 count=0;
 count1++;
  }

  }
 for(int i=0;i<count1;i++)
  System.out.println(srr[i]+"  "+rev[i]);
  }
  }