如何使用索引?请帮助初学者



所以我想知道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?
}
}

如果你不知道一个方法做了什么,那么解决方案是去看看它做了什么。例如,java文档会告诉你

public int indexOf(int ch)

返回此字符串中第一次出现的指定字符的索引

在任何一种情况下,如果此字符串中没有出现此类字符,则返回 -1。

你如何使用它不一定是错误的,考虑到如果找不到字符,该方法如何返回 -1。但是,如果您想检查用户输入的单词中有多少元音,则检查他们输入的单词是否在元音字符串中是不对的。

所有标准的Java库,类和方法都有Javadoc来描述它们的作用。

您需要做的就是查找Javadoc,然后他们对其进行了描述。

在这种情况下,Javadoc位于:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)

您解决此类问题的第一步应该始终是文档,然后如果这不起作用,请尝试进行网络搜索以查找示例。例如 5 秒在谷歌上输入"java index的例子"找到我:http://www.tutorialspoint.com/java/java_string_indexof.htm

然后,如果这不起作用,您可以尝试在此处提出问题。

当方法名称前有单词boolean时,这意味着该方法将返回值true或值false。 你的程序打印出来的正是这个truefalse值,与"这个词有"在同一行。

如果您传递给它的字符是元音,则此特定方法将返回true,否则false。 方法indexOf告诉您String的哪个字符是第一个等于传递给该方法的值的字符。 它为第一个字符返回0,为第二个字符返回1,依此类推。 如果没有匹配的字符,它将返回-1。 在这种情况下,您只是检查 indexOf 返回的值是否-1 - 换句话说,字符是否在String "AEIOUaeiou"中。

indexOf(String str) 返回此字符串中指定子字符串第一次出现的索引。如果不存在这样的 str 值,则返回 -1。

例如:

int num1 = "AEIOUaeiou".indexOf("a");//它给出 5

int num2 = "AEIOUaeiou".indexOf("A");//它给出 0

int num3 = "AEIOUaeiou".indexOf("z");//它给出 -1

1 不要那样做!在 main 中创建一个扫描程序,使用它读取输入,然后调用您的方法。

2 countVowels(input)怎么样?您需要编写一个static int countVowels(String input)方法。

3 返回 true,因为您传入'a'

4 见第3条。

5 请参阅第 2 条,并添加一个static boolean isVowel(char a)

这是 indexOf 方法的作用

string.indexOf(searchvalue,start)

参数

searchvalue : Required. The string to search for 
start       : Optional. Default 0. At which position to start the search

返回值

Number : The position where the specified searchvalue occurs for the first time, or -1 if it never occurs

简单来说,方法索引检查从起始位置(如果指定)传递给它的值的第一次出现,并返回在字符串中首次遇到该值的位置。

例如。

String s = "AEIOUaeiou";
s.indexOf("a");     //This would get a value of 5.
s.indexOf("v");     //This would get a value of -1, since it doesn't have the character v

为了回答您的问题,

  1. 您可以直接将扫描仪声明为私有扫描程序,并在整个程序

    `private static Scanner input = new Scanner(System.in);`
    
  2. 您可以编写一个接收用户输入的字符串的方法然后检查字符串是否包含任何元音。您可以使用 indexOf 或 包含方法 检查每个元音方法的索引。

  3. 上面已经描述过了。

  4. 更好的方法如下。

公共类元音计数器{

    public static void main (String[] args) {
      Scanner keyboard = new Scanner (System.in); // No need to declare it as global. You use it only once.
      System.out.println ("Enter word : ");   //Prompt the user to enter a word    
      String input = keyboard.nextLine ();  //Fetch the word that the user enters into a String 
      System.out.println ("This word has" + countVowel (input)); // Pass the string to the method to check if it has vowels.
    }
   private static int countVowel (String a) {
     int count = 0;
     String s =  a.toLowerCase ();    // convert the string to lower case so that you only have to check for the lower case characters
    // Here you would need to check the number of times each vowel exists in the String and incremenet the count everytime.
    return count;
   }  
}

最新更新