【问题标题】:How to use indexOf? Please help a beginner out如何使用 indexOf?请帮助初学者
【发布时间】: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


【解决方案1】:

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

公共 int indexOf(int ch)

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

无论哪种情况,如果该字符串中没有出现这样的字符,则返回-1。

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

【讨论】:

    【解决方案2】:

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

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

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

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

    如果这不起作用,您可以尝试在这里提问。

    【讨论】:

      【解决方案3】:

      如果在方法名称前有 boolean 一词,则表示该方法将返回值 true 或值 false。您的程序正在打印出的正是这个 truefalse 值,与“This word has”在同一行。

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

      【讨论】:

        【解决方案4】:

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

        例如:

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

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

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

        【讨论】:

          【解决方案5】:

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

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

          3 在您传入'a' 后返回true。

          4见编号3

          5查看编号2,并添加static boolean isVowel(char a)

          【讨论】:

          • 哇,回顾我的旧代码。看看我走了多远有点酷:')
          【解决方案6】:

          下面是 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 或 contains 方法来检查每个元音使用 indexOf 方法。

          3. 上面已经描述过了。

          4. 更好的方法如下。

          公共类 vowelCounter{

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-01-10
            • 1970-01-01
            • 1970-01-01
            • 2021-04-11
            • 2022-01-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多