【问题标题】:find the duplicate word from a sentence with count using for loop使用for循环从带有计数的句子中找到重复的单词
【发布时间】:2015-12-24 05:39:50
【问题描述】:

由于我是 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

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

【问题讨论】:

  • 你可以使用地图吗?
  • @redflar3 没有。只能使用 for 循环。
  • 你不能在这些循环中给出打印,因为我们无法找到给定单词的最后一个匹配项。由于同样的原因,我们必须有一些机制来以某种形式存储事件。一个想法的方法是创建一个 Map,每当找到匹配项时,将值存储在 map 中,键为 word,值为 1,如果 word 已经在 Map 中,则可以增加值。
  • 另外,OP 中提到的输出并不是真正的输出,因为您只使用了一个 count 变量,无论单词如何,它都会保持递增。
  • @redflar3 对于这段代码,我得到的输出与预期不同。

标签: java for-loop count duplicates


【解决方案1】:

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

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 &lt; i) { break; }

i 是我们计算重复词的索引,j 是我们与之比较的词。由于我们总是从头开始,我们知道如果 j &lt; i 中的单词相等,则它已经在外循环的早期运行中被处理过。

在这种情况下,使用break,我们中断了内部循环,流程在外部循环中继续。由于我们根本没有更新count,它仍然为零,因此打印结果if (count &gt; 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

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

【讨论】:

  • @Sva.Mu 你能说if (j &lt; i) { break; }这里发生了什么
  • 试图在代码 cmets 中解释,但给我几分钟,我会更新答案并提供更多细节。
  • 即确保已打印的单词不会再次计数。你为什么不删除这些行,看看会发生什么......放一些额外的打印语句来查看执行 \flow..
  • 对答案添加了一些更详细的解释,希望它能澄清更多。
【解决方案2】:

先在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 循环。
  • 检查外面的情况
  • 在这样做之后,我会像`单词 hi 出现 2 次单词 hello 出现 2 次单词 hi 出现 2 次单词 hello 出现 2 次' 我只需要 the word hi occured 2 time the word hello occured 2 time
  • 检查我已编辑,这肯定会起作用,我试过了,抱歉给您带来不便
【解决方案3】:
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]);
  }
  }

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多