下面是我爲簡單數組問題的代碼,我必須將一個字符串放入一個數組(完成),然後掃描數組以獲得唯一的單詞,將它們放入單獨的數組中,然後計算多少在第三個和最後一個數組中的字符串中存在單獨的唯一字。我可以得到它打印出字符串,但它並計算每個單詞但不是唯一的單詞。我相信問題在於if語句或uwords增量。 任何幫助表示讚賞,但我寧願暗示的答案,而不僅僅是如果可能的答案!使用其他數組的數組增量使用
String[] words = text.split(" ");
String[] uwords = new String[words.length];
int[] wordcount = new int[words.length];
for(int i=0; i<words.length; i++){
for(int e = 0; i<words.length; e++){ //loop to count individual words
if(words[i].equals(uwords[e])){ //If word in array is contained in the unique words arrary then increment
wordcount[wordcounter] = wordcounter+ 1;
}else{ //else (word is not contained in uwords) add to uwords and increment
uwords[e] = words[i];
wordcount[i] = wordcount[ + 1];
}
}
System.out.println(uwords[i] + ": " + wordcount[i]);
Pesudocode是:
Find unique words:
Create unique word array
Create unique word count array
For every word in text
if word exists in unique word array
Increment corresponding count in unique word count array
else
Add new unique word in unique word array
Increment corresponding count in unique word count array
List all unique words
在布穀鳥藍莓蒼蠅,並在收費橋下哈希派所在。 –