【发布时间】:2014-11-25 08:27:25
【问题描述】:
这样做的目的是从用户那里得到一个句子并确定每个元音出现了多少。除了我不确定如何忽略大写和小写字母但我猜是 equalsIgnoreCase 或 toUpperCase ()。
我还想知道是否有其他方法可以使用String、StringBuilder 或Character 的其他类来执行此操作。我对编程还是很陌生,这一章让我很生气。
int counterA=0,counterE=0,counterI=0,counterO=0,counterU=0;
String sentence=JOptionPane.showInputDialog("Enter a sentence for vowel count");
for(int i=0;i<sentence.length();i++){
if(sentence.charAt(i)=='a'){
counterA++;}
else if(sentence.charAt(i)=='e'){
counterE++;}
else if(sentence.charAt(i)=='i'){
counterI++;}
else if(sentence.charAt(i)=='o'){
counterO++;}
else if(sentence.charAt(i)=='u'){
counterU++;}
}
String message= String.format("The count for each vowel is \nA:%d\nE:%d\nI:%d\nO:%d\nU:%d",
counterA,counterE,counterI,counterO,counterU);
JOptionPane.showMessageDialog(null, message);
}
}
代码在这里
【问题讨论】:
-
if (theChar == 'a' || theChar == 'A')? -
Character.toLowerCase(sentence.charAt(i))=='a')