Java:write方法,接受字符串对象作为参数并返回字数

时间:2014-04-06 22:10:41

标签: java word-count

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

3 个答案:

答案 0 :(得分:0)

首先,您需要清理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)));

然后你的代码为我工作。

答案 1 :(得分:0)

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

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

}

答案 2 :(得分:-1)

" counter"声明?你的其余代码在哪里?您声明了自己的方法,但没有打电话给他们。同样在你的第一个if语句中我相信你打算打电话给getWordCount(input)并打印出结果。修复这些问题会相应地更新您的帖子。