2016-09-02 13 views
-5

このコードには、任意の柔軟性のある単語の出力が必要です。 このコードは何が欠けていますか?助けてください。このコードの任意の単語に単一の文字をどのように取得しますか(java)

スペーシングワードと任意のタイプのワード。 このコードは基本的なプログラミングのためのものです。

String str = "Programming"; 
    char searchChar; 
    int counter = 0; 

    for (int i = 0; i < str.length(); i++) { 
     searchChar = str.charAt(i); 
     i++; 

     for (int j = 0; j < str.length(); j++) { 
      if (searchChar == str.charAt(j)) { 
       counter = counter + 1; 
       continue; 
      } 
     } 

     if (counter > 1) { 
      System.out.println(searchChar); 
     } 

     counter = 0; 
    } 
+2

あなたが何を求めているのかわかりません。 – ChiefTwoPencils

+4

"このコードでは、このコードが不足している言葉の柔軟性の出力が必要ですか?助けてください スペーシングワードと任意のタイプのワードを使用しています。 --- ** ...何ですか?** – byxor

+0

達成したいのは何ですか? –

答えて

-1

あなたのコメントによると、あなたは文字列における文字の頻度をカウントします。あなたはそれの文字の数を維持するためにmapを使用することができます。それは簡単だろう。

Map<Character, Integer> charFrequency = new HashMap<>(); 
String str = " I like programming"; 

// To count frequency of letters 
for (char ch : str.toCharArray()) { 
    Integer val = charFrequency.get(ch); 
    if (val != null) { 
     charFrequency.put(ch, val + 1); // letter exists in map, increase frequency by 1 
    } else { 
     charFrequency.put(ch, 1); // letter does not exist in map, set frequency to 1 
    } 
} 

// Print the frequencies of letters 
for (Map.Entry<Character, Integer> entry : charFrequency.entrySet()) { 
    Character ch = entry.getKey(); 
    Integer frequency = entry.getValue(); 
    System.out.println("'" + ch + "' has frequency = " + frequency); 
} 
関連する問題