How do I check if a char is a vowel?

前端 未结 8 1108
醉酒成梦
醉酒成梦 2020-11-30 13:54

This Java code is giving me trouble:

    String word = 
    int y = 3;
    char z;
    do {
        z = word.charAt(y);
         if (z!=         


        
8条回答
  •  死守一世寂寞
    2020-11-30 14:08

    I have declared a char[] constant for the VOWELS, then implemented a method that checks whether a char is a vowel or not (returning a boolean value). In my main method, I am declaring a string and converting it to an array of chars, so that I can pass the index of the char array as the parameter of my isVowel method:

    public class FindVowelsInString {
    
            static final char[] VOWELS = {'a', 'e', 'i', 'o', 'u'};
    
            public static void main(String[] args) {
    
                String str = "hello";
                char[] array = str.toCharArray();
    
                //Check with a consonant
                boolean vowelChecker = FindVowelsInString.isVowel(array[0]);
                System.out.println("Is this a character a vowel?" + vowelChecker);
    
                //Check with a vowel
                boolean vowelChecker2 = FindVowelsInString.isVowel(array[1]);
                System.out.println("Is this a character a vowel?" + vowelChecker2);
    
            }
    
            private static boolean isVowel(char vowel) {
                boolean isVowel = false;
                for (int i = 0; i < FindVowelsInString.getVowel().length; i++) {
                    if (FindVowelsInString.getVowel()[i] == vowel) {
                        isVowel = true;
                    }
                }
                return isVowel;
            }
    
            public static char[] getVowel() {
                return FindVowelsInString.VOWELS;
            }
        }
    

提交回复
热议问题