由于我是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
请帮助我获得我期待的输出。请解释一下。所以我也能理解 提前致谢
答案 0 :(得分:4)
您只需要为外循环打印结果。此外,您需要避免检查在上一次迭代中已经检查过的单词:
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
希望我没有使这个例子更复杂
答案 1 :(得分:0)
首先打印外部循环,这将执行技巧并在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");
}
答案 2 :(得分:0)
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]);
}
}