【发布时间】:2013-12-24 07:52:00
【问题描述】:
所以我想知道indexOf() 做了什么。当我想在我的程序中使用它时,它会找出用户输入的单词中有多少个元音。
public static boolean methodCheck(char a){
return "AEIOUaeiou".indexOf(a) != -1;
}
但这似乎根本不起作用哈哈。因为我不知道indexOf() 实际上做了什么。无论如何,这是我到目前为止的程序(对不起,如果它不好,我真的是新人)。我也留下了 5 个问题,这对我很有帮助!请并感谢您的帮助:D
import java.util.Scanner;
public class vowelCounter {
private static String input = methodInput(); //1. is there any other way to make a global Scanner?
public static void main(String[] args){
System.out.println("Enter word");
System.out.println(input);
System.out.println("This word has" + methodCheck('a')); //2. what should i put in my parameters?
}
public static boolean methodCheck(char a){
return "AEIOUaeiou".indexOf(a) != -1; //3. what does this line do?
}
public static String methodInput(){
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
return input;
//4. the output is 'hastrue' why is that?
//5. how can i make this program better?
}
}
【问题讨论】:
-
Returns the index within this string of the first occurrence of the specified character.docs.oracle.com/javase/7/docs/api/java/lang/… -
这是基本的东西;请在此处发布问题之前进行一些研究.... 只需在 indexOf 上进行谷歌搜索即可获取您想要的结果..
-
首先,您应该使用有意义的名称。例如,
methodCheck看起来就像您想要的isVowel。接下来,您可以考虑使用countVowels方法来使用isVowel。 -
@MiteshPathak 对你来说基本的东西可能对其他人来说不是基本的。人们可能会争论任何关于语法的任何问题都是“基本”的东西,因为无论如何它都(理想情况下)记录在案。
标签: java methods char java.util.scanner