I am asked to write a program to print the number of vowels in word, but when I do that, it won't print the number of vowels, but it just kind of lists the numbers but not the sum. Can anyone help me to see what's wrong or how can I fix it? Thanks very much!
package vowel2;
import java.util.Scanner;
public class Vowel2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = in.next();
int v = 0;
for(int i = 0;i<word.length();i++)
{
char ch = word.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
v++;
}
System.out.println(v);
}
}
}
Running the program:
Enter a word: happy
0
1
1
1
1
BUILD SUCCESSFUL (total time: 3 seconds)
I want the output to be 1...
Aucun commentaire:
Enregistrer un commentaire