【问题标题】:Java: write method that accepts string object as argument and returns word countJava:接受字符串对象作为参数并返回字数的写入方法
【发布时间】:2018-04-15 17:10:46
【问题描述】:

java 的任务是编写一个接受字符串对象作为参数并返回它包含的单词数的方法。在要求用户输入字符串并将其传递给方法的程序中演示该方法。字数应显示在屏幕上。我知道它很接近,但可能有一些错误。

public class WordCounter
{

    //Asks and gets the users input here
    private static string getInput(Scanner in)
    {
        String input;

        //Imported scanner here
        Scanner in = new Scanner(System.in);

        //Ask the user to enter string here
        System.out.println("Enter a string here: ");
        input = in.nextLine();

        //Create an if/else statment to find out if the user entered input
        if(input.length() > 0)
        {
            getInput(input);    //Get word count
        }
        else
        {
            System.out.println("Error -- You must enter a string!");
            System.out.println("Enter a string here: ");
            input = in.nextLine();
        }

        return input;

    }   //Close public static string getInput here



    //Calculates the number of words the user inputs
    public static int getWordCount(String input)
    {
        String[] result = input.split(" ");

        return result.length;

    }   //Close public static int getWordCount here     



    //Prints out the number of words from the users input in string above
    public static void main(String[] args)
    {

        //Print out the number of words within the users string here
        System.out.println("The number of words in the string are: " + counter);

    }   //Close public static void main string args here

【问题讨论】:

标签: java word-count


【解决方案1】:

我修复了你的很多部分代码,并评论了我改变的地方。

import java.util.Scanner;

public class WordCounter
{
//Asks and gets the users input here
private static String getInput(Scanner in) // java is CASE-SENSITIVE. string !=String
{
    String input;

    //Imported scanner here
    // Scanner in = new Scanner(System.in); // cannot redefine variable in same scope, also un-needed
    //Ask the user to enter string here
    System.out.println("Enter a string here: ");
    input = in.nextLine();

    //Create an if/else statment to find out if the user entered input
    if(input.isEmpty()) // better form to do this
    {
        System.out.println("Error -- You must enter a string!");
       return getInput(in);    //Get input again (it's bad form to use recursion here, but not technically wrong)
    }
    else
    {
        return input;
    }
}   //Close public static string getInput here



//Calculates the number of words the user inputs
private static int getWordCount(String input)
{
    String[] result = input.split(" ");
    return result.length;
}   //Close public static int getWordCount here     



//Prints out the number of words from the users input in string above
public static void main(String[] args)
{
    //Print out the number of words within the users string here
    String input = getInput(new Scanner(System.in)); //You needed to actually call these methods!
    int counter = getWordCount(input);
    System.out.println("The number of words in the string are: " + counter);
}   //Close public static void main string args here

}

【讨论】:

  • getInput(in); 不只是糟糕的形式......因为你没有return getInput(in);
  • @ElliottFrisch 很好,我错过了。
【解决方案2】:

首先,你需要清理你的 getInput() 方法 -

private static String getInput(Scanner in) {
  // Ask the user to enter string here
  do {
    System.out.println("Enter a string here: ");
    String input = in.nextLine();

    // Create an if/else statement to find out if the user
    // entered input
    if (input.trim().length() > 0) {
      return input.trim();
    } else {
      System.out.println("Error -- "
          + "You must enter a string!");
    }
  } while (true);
} // Close public static string getInput here

其次,在您的 main 方法中 - 声明并初始化您的 counter -

int counter = getWordCount(getInput(new Scanner(System.in)));

然后你的代码对我有用。

【讨论】:

    【解决方案3】:

    “计数器”在哪里声明?您的其余代码在哪里?你声明你的方法但不调用它们。同样在您的第一个 if 语句中,我相信您的意思是调用 getWordCount(input) 并打印出结果。解决这些问题,相应地更新您的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 2016-10-06
      相关资源
      最近更新 更多