子例程总是 returns -1 值,我该如何重新排列以使其发挥作用?

Subroutine always returns the -1 value , how do I rearrange to make it function?

/ * 查找单词中的第一个元音及其位置 returns * */


public static int findFirstVowel (String word) {
    int consonant = 0;
    for(int count = 0; count < word.length(); count++){
        word.toUpperCase();
   char letter1 = word.charAt(count);
   String letter2 = (Character.toString(letter1));
   if (isVowel(letter2) == true){
    //consonant = 0;
    return (count);
     }
    }
    return (-1);
}**

您的问题出在 word.toUpperCase() 上。由于字符串在 Java 中是不可变的,因此这将创建一个您未使用的新大写字符串。做你想做的事情的正确方法是:

public static int findFirstVowel (String word) {
    int consonant = 0;
    word = word.toUpperCase(); // you need to set word back to the uppercase version

    for (int count = 0; count < word.length(); count++) {
        char letter1 = word.charAt(count);
        String letter2 = (Character.toString(letter1));

        if (isVowel(letter2)) {
            return count;
        }
    }

    return -1;
}