使用字符串方法查找和计算字符串中的元音?

我有这个家庭作业的问题(我是诚实的,至少不试图隐藏它)而且我在弄清楚如何去做这件事时遇到了问题。

鉴于以下声明:String phrase =“WazzUp? – 谁是第一个??? – IDUNNO”; 编写必要的代码来计算字符串中元音的数量,并将适当的消息打印到屏幕上。

这是我到目前为止的代码:

String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO"; int i, length, vowels = 0; String j; length = phrase.length(); for (i = 0; i < length; i++) { j = phrase.substring(i, i++); System.out.println(j); if (j.equalsIgnoreCase("a") == true) vowels++; else if (j.equalsIgnoreCase("e") == true) vowels++; else if (j.equalsIgnoreCase("i") == true) vowels++; else if (j.equalsIgnoreCase("o") == true) vowels++; else if (j.equalsIgnoreCase("u") == true) vowels++; } System.out.println("Number of vowels: " + vowels); 

但是,当我运行它时,它只会产生一堆空白行。 有人可以帮忙吗?

phrase.substring(i, i++); 应该是phrase.substring(i, i + 1);

i++给出i++的值,然后加1。 正如你现在所拥有的那样, String j实际上是phrase.substring(i, i); ,这总是空字符串。

您不需要在for循环体中更改i的值,因为它已经递增for (i = 0; i < length; i++)

这可能/应该是一个单行

 System.out.println("the count of all vowels: " + (phrase.length() - phrase.replaceAll("a|e|i|o|u", "").length())); 

和它的String方法

我不认为需要在循环中使用print语句。

 String s = "Whatever you want it to be.".toLowerCase(); int vowelCount = 0; for (int i = 0, i < s.length(); ++i) { switch(s.charAt(i)) { case 'a': case 'e': case 'i': case 'o': case 'u': vowelCount++; break; default: // do nothing } } 

这会将字符串转换为小写,并检查字符串中的所有字符是否有元音。

改进以上答案以考虑大写元音:

 System.out.println(s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()); 

在使用之后i ++增量i,所以你实际上是在说string.substring(i,i)。 由于结束标记是独占的,因此总是返回一个空字符串。 一个简单的解决方法是将其更改为

 j = phrase.substring(i, ++i); 

希望有所帮助!

使用集合:

  String word = "busiunesuse"; char[] wordchar= word.toCharArray(); Character allvowes[] ={'a','e','i','o','u'}; Map mymap = new HashMap(); for(Character ch: wordchar) { for(Character vowels : allvowes) { if(vowels.equals(ch)) { if(mymap.get(ch)== null) { mymap.put(ch, 1); } else { int val = mymap.get(ch); mymap.put(ch, ++val); } } } } Set  myset = mymap.keySet(); Iterator myitr = myset.iterator(); while(myitr.hasNext()) { Character key = (Character) myitr.next(); int value = mymap.get(key); System.out.println("word:"+key+"repetiotions:"+value); } } 

}

我知道这是一个旧编辑,但我认为如果你使用和数组元音更好:

 public static void main(String[] args) { String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO".toLowerCase(); int vowels = 0; String[] array = {"a", "e", "i", "o", "u"}; for (int i = 0; i < phrase.length(); i++) { String j = phrase.substring(i, i + 1); System.out.println(j); for (int n = 0; n < array.length; n++) { if (j.equals(array[n])) { vowels++; } } } System.out.println("Number of vowels: " + vowels); } 

计算字符串中的元音数量

 class Test { static int a; public static String countVowel(String strName) { char ch[] = strName.toCharArray(); for(int i=0; i 

公共类JavaApplication2 {

 /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here System.out.println("Enter some text"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine().toLowerCase(); char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'}; int[] countVowel = new int[5]; for (int j = 0; j < input.length(); j++) { char c =input.charAt(j); if(c=='a') countVowel[0]++; else if(c=='e') countVowel[1]++; else if(c=='i') countVowel[2]++; else if(c=='o') countVowel[3]++; else if(c=='u') countVowel[4]++; } for (int i = 0; i