[英]My program will not count the vowels
I have a homework assignment that needs to count the vowels in the string.我有一个作业需要计算字符串中的元音。 My program is running, but will not ignore consonants and spaces.
我的程序正在运行,但不会忽略辅音和空格。 Is there something I am missing in the code?
我在代码中遗漏了什么吗?
static void main(String[] args) {
// TODO Auto-generated method stub
boolean stopProgram = false;
while(!stopProgram == true) {
System.out.println("Enter a Word or \"q\" to end the program: "); // prompt the user for input
Scanner wordScanner = new Scanner(System.in);
String word = wordScanner.nextLine();
if(!word.equals("q")) {
String vowels[] = {"a","e","i","o","u","y","A","E","I","O","U","Y"}; //define upper and lower case vowels
int count = 0;
for(int i = 0; i < word.length(); i++) {
if(String.valueOf(word.charAt(i)).equals(vowels[0]) ||
String.valueOf(word.charAt(i)).equals(vowels[1]));
String.valueOf(word.charAt(i)).equals(vowels[2]);
String.valueOf(word.charAt(i)).equals(vowels[3]);
String.valueOf(word.charAt(i)).equals(vowels[4]);
String.valueOf(word.charAt(i)).equals(vowels[5]);
String.valueOf(word.charAt(i)).equals(vowels[6]);
String.valueOf(word.charAt(i)).equals(vowels[7]);
String.valueOf(word.charAt(i)).equals(vowels[8]);
String.valueOf(word.charAt(i)).equals(vowels[9]);
String.valueOf(word.charAt(i)).equals(vowels[10]);
String.valueOf(word.charAt(i)).equals(vowels[11]);
count++;
}
System.out.println(("The Vowel Count is: ") + count);
}
}
}
} }
That's your if-statement:那是你的 if 语句:
if(String.valueOf(word.charAt(i)).equals(vowels[0]) ||
String.valueOf(word.charAt(i)).equals(vowels[1]));
You break the if statement by the ;
你用
;
打破 if 语句;
at the very end.在最后。
After that, there are a lot of statements (whose result is ignored)之后,有很多语句(忽略其结果)
String.valueOf(word.charAt(i)).equals(vowels[1]));
which are not part of the if statement anymore.不再是 if 语句的一部分。
Then, you increment count with every iteration - regardless of the if, it's interrupted by said ;
然后,每次迭代都会增加计数 - 无论是否,它都会被 say 中断
;
. .
First you should make your code a bit cleaner : create your list of vowels outside of your while so that you won't create it at each iteration, then use maybe a double for loop to run through your tab of vowels :首先,您应该使您的代码更简洁:在 while 之外创建元音列表,这样您就不会在每次迭代时都创建它,然后使用双循环来遍历元音选项卡:
static void main(String[] args) {
// TODO Auto-generated method stub
boolean stopProgram = false;
String vowels[] = {"a","e","i","o","u","y","A","E","I","O","U","Y"}; //define upper and lower case vowels
while(!stopProgram == true) {
System.out.println("Enter a Word or \"q\" to end the program: "); // prompt the user for input
Scanner wordScanner = new Scanner(System.in);
String word = wordScanner.nextLine();
if(!word.equals("q")) {
int count = 0;
for(int i = 0; i < word.length(); i++) {
for(int j = 0; j < 12; ++j){
if(String.valueOf(word.charAt(i)).equals(vowels[j]){
++count;
break;
}
}
}
System.out.println(("The Vowel Count is: ") + count);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.