亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算句子中重復單詞的總數

計算句子中重復單詞的總數

aluckdog 2023-07-28 15:51:39
我想計算句子中重復單詞或重復單詞的總數。在這里我可以打印單詞但無法計算這些單詞。import java.util.*;public class Duplicate {          public static void main(String[] args) {                 String input = "Big black bug bit a big black dog on his big black nose";          //Scanner scanner = new Scanner(System.in);         //System.out.println("Enter the sentence");                //String input = scanner.nextLine();                int count;         int counter=0;                 //Converts the string into lowercase          input = input.toLowerCase();                    //Split the string into words using built-in function          String words[] = input.split(" ");                    System.out.println("Duplicate words in a given string : ");           for(int i = 0; i < words.length; i++) {              count = 1;              for(int j = i+1; j < words.length; j++) {                  if(words[i].equals(words[j])) {                      count++;                      //Set words[j] to 0 to avoid printing visited word                      words[j] = "0";                      counter=counter+1;                                    }              }                            //Displays the duplicate word if count is greater than 1              if(count > 1 && words[i] != "0")  {                System.out.println(words[i]);            }                    }                  System.out.println("Total Duplicate words in a given string : " +counter);}  }我期望輸出:--給定字符串中的重復單詞:big black給定字符串中的重復單詞總數:2輸出如下:給定字符串中的重復單詞:big black給定字符串中的重復單詞總數:10總計數顯示 10,而不是 2。
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

將增量移動到counter此處應該可以:


if (count > 1 && !words[i].equals("0")) {

? ? counter++;

? ? System.out.println(words[i]);

}

而不是在第二個循環中。counter每次您發現另一個重復項時,現在都會增加。counter僅當您發現存在重復單詞時,向下移動才會增加它。


也就是說,可以通過使用Map來計算每個單詞出現的次數來簡化此方法。



查看完整回答
反對 回復 2023-07-28
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

嘗試使用 1 個HashMap而不是 2 個 for 循環,如下所示


public static void main(String[] args) {  


    String input = "Big black bug bit a big black dog on his big black nose";  

    HashMap<String, Integer> dupCount = new HashMap<>();


       //Converts the string into lowercase  

    input = input.toLowerCase();  


    //Split the string into words using built-in function  

    String words[] = input.split(" ");  



    System.out.println("Duplicate words in a given string : ");   

    for(int i = 0; i < words.length; i++) { 

        if(dupCount.containsKey(words[i])){

            int cnt = dupCount.get(words[i]);

            cnt = cnt + 1;

            dupCount.put(words[i], cnt);        

        }else{

            dupCount.put(words[i], 0);

        }

    }


    for(Map.Entry<String, Integer> test : dupCount.entrySet()){

        if(test.getValue() > 1)  {

            System.out.println("Duplicate words in a given string : " +test.getKey() + " : " + test.getValue());

        }

    }

}

在這種情況下,每次出現重復時都會打印最后一條語句,您可以根據需要對其進行修改。


查看完整回答
反對 回復 2023-07-28
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號