Use string methods to find and count vowels in a string?

前端 未结 9 2087
青春惊慌失措
青春惊慌失措 2020-12-02 02:04

I have this problem for homework (I\'m being honest, not trying to hide it at least) And I\'m having problems figuring out how to do it.

Given the following declarat

9条回答
  •  無奈伤痛
    2020-12-02 02:26

    I know that's an old edit , but i think it's better if you use and array for the vowels :

    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);
    }
    

提交回复
热议问题