Java 子例程总是返回-1值,如何重新排列以使其正常工作?
/ *查找单词中的第一个元音并返回其位置 * */Java 子例程总是返回-1值,如何重新排列以使其正常工作?,java,subroutine,Java,Subroutine,/ *查找单词中的第一个元音并返回其位置 * */ public static int findFirstVowel(字符串字){ int辅音=0; 对于(int count=0;count
public static int findFirstVowel(字符串字){
int辅音=0;
对于(int count=0;count
您的问题在于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;
}
public static int findFirstVowel(字符串字){
int辅音=0;
word=word.toUpperCase();//您需要将word设置回大写版本
对于(int count=0;count
if(isvouel(letter2)==true){
->if(isvouel(letter2)){
。没有必要像这样比较布尔值。
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;
}