繁体   English   中英

计算字符串中的单词并从方法中调用它?

[英]Count words from a string and call it from a method?

这是我到目前为止的代码,我想要做的是调用我创建的方法,以向用户显示用户输入中有多少个单词。 我不确定我在说什么是错的还是什么,但是我是否可以从知道他们在做什么的人那里得到一些帮助。

import java.util.Scanner;

public class Chapter3Question5 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Type some words: ");
        String s = in.nextLine();
        System.out.println("There are " + (countWords) + " words.");
        //want to call the method here and print the results

        in.close();
    }

    public static int countWords(String str) {
        int wordCount = 0;

        boolean word = false;
        int endLine = str.length() - 1;

        for (int i = 0; i < str.length(); i++) {

            if (Character.isLetter(str.charAt(i)) && i != endLine) {
                word = true;
            } else if (!Character.isLetter(str.charAt(i)) && word) {
                wordCount++;
                word = false;
            } else if (Character.isLetter(str.charAt(i)) && i == endLine) {
                wordCount++;
            }
        }
        return wordCount;
    }
}

在您的情况下,您需要通过其name和好的parameters来调用您的方法:

String s = in.nextLine();
System.out.println("There are " + countWords(s) + " words.");

一部分是,您的方法很好,而且行得通


提示(只是为了让您知道另一种方法),将执行以下操作:

public static int countWords(String str) {
    return str.split("\\s+").length;
}

它将在有空格的地方拆分您的句子,并创建一个数组(这样的单词数组)并取其长度:单词数

您没有正确调用该方法:

System.out.println("There are " + (countWords) + " words.");

应该成为

System.out.println("There are " + countWords(s) + " words.");

同样,您也可以尝试通过在空格上分割字符串并在输出数组上使用length()方法来做同样的事情。 这将使您的方法不那么复杂。

请参阅azro的答案。

我想你是说

import java.util.Scanner;

public class Chapter3Question5 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Type some words: ");
String s = in.nextLine();
System.out.println("There are " + countWords(s) + " words.");
//want to call the method here and print the results

in.close();
}
 public static int countWords(String str)
{
 int wordCount = 0;

    boolean word = false;
    int endLine = str.length() - 1;

    for (int i = 0; i < str.length(); i++) {

        if (Character.isLetter(str.charAt(i)) && i != endLine) {
            wordCount++;
            word = true;

        } else if (!Character.isLetter(str.charAt(i)) && word) {
            wordCount++;
            word = false;

        } else if (Character.isLetter(str.charAt(i)) && i == endLine) {
            wordCount++;
        }
    }
    return wordCount;
}
}

您必须正确调用方法。 它需要参数,而您尚未传递任何参数。 另外,方法调用上没有括号。 因此应该是:

System.out.println("There are " + (countWords(s)) + " words."); // add parentheses and parameters

附带说明一下,可以像下面这样轻松实现功能:

public class Chapter3Question5 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Type some words: ");
        String s = in.nextLine();
        System.out.println("There are " + (countWords(s)) + " words.");
        //want to call the method here and print the results

        in.close();
    }

    public static int countWords(String str) {
        return str == null || str.length() == 0 ? 0 : str.split("\\s+").length;
    }
}

在这里,我将字符串用空格分开,从而得到单词数组。 接下来返回数组的长度。

测试和输出:

输入一些单词:
你好,世界再见
有4个字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM