当前位置: 首页 > 知识库问答 >
问题:

使用方法计算字符串中的单词

公羊凌
2023-03-14

我有一个项目,制作一个程序,将字符串作为输入,然后打印字符串中的字数作为输出。在这方面,我们应该使用3种方法,一种读取输入,一种打印输出,一种计算单词。

我知道我错过了一些基本的东西,但是我已经花了几个小时在这上面,不知道为什么程序不能正常运行。我需要保持程序非常简单,所以我不想编辑太多,只要找到问题并修复它,这样它就能正常运行。

示例:输入一串文本:敏捷的棕色狐狸跳过了懒狗。9个字

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter text: ");
    String words = wordInput(in);
    int count = wordCount(words);
    System.out.println(words);
    System.out.println(count);
    printLine(count);
}

private static String wordInput(Scanner in)
{
    String words = in.nextLine();
    return words;
}
private static int wordCount(String words)
{
    int length = words.length();
    int ctr = 0;
    int spot = 0;
    int stop = 1;
    char space = ' ';
    char end = '.';
    char com = ',';
    char yes = '!';
    char question = '?';

    while (length > 0 && stop > 0)
    {
        if (words.charAt(spot) == space)
        {
            ctr++;
            spot++;
        }
        else if (words.charAt(spot) == com)
        {
            spot++;
        }
        else if (words.charAt(spot) == yes || words.charAt(spot) == end || words.charAt(spot) == question)
        {
            stop = -1;
        }
        else if (spot > length)
        {
            stop = -1;
        }
        else
            spot++;
    }
    return ctr + 1;
}
private static void printLine(int ctr)
{
    System.out.println(ctr + " words");
}

共有2个答案

太叔京
2023-03-14

假设输入字符串的人不会出现语法错误,请尝试在所有空格处拆分字符串,并将其设置为字符串[]。下面是一个示例

    String temp = JOptionPane.showInputDialog(null, "Enter some text here");
    String[] words = temp.split(" ");
    JOptionPane.showMessage(null, String.format("Words used %d", words.length));
乐宜民
2023-03-14

这是一个重写的字数,可以根据您的要求对代码进行最小的更改。然而,我不确定它是否能产生你所期望的答案。

private static int wordCount(String words)
{
    int length = words.length();
    int ctr = 0;
    int spot = 0;
    char space = ' ';
    char end = '.';
    char com = ',';
    char yes = '!';
    char question = '?';

    while (spot < length)
    {
        if (words.charAt(spot) == space)
        {
            ctr++;
            spot++;
        }
        else if (words.charAt(spot) == com)
        {
            spot++;
        }
        else if (words.charAt(spot) == yes || words.charAt(spot) == end || words.charAt(spot) == question)
        {
            break;
        }
        else
            spot++;
    }
    return ctr + 1;
}

然而,从根本上讲,你是在试图数一串单词,这是一种已知的艺术。一些选项和进一步阅读:

  • http://www.quickprogrammingtips.com/java/find-number-of-words-in-a-string-in-java.html
  • 在字符串方法中计算单词?

这导致了以下更简单的结果:

private static int wordCount(String words) {
    return words.split("\\s+").length;
}
 类似资料:
  • 问题内容: 我想知道如何编写一种仅通过使用charAt,length或substring之类的字符串方法来计算Java字符串中单词数的方法。 循环和if语句还可以! 我非常感谢我能获得的任何帮助!谢谢! 问题答案:

  • 问题内容: 我认为我已经很好地理解了这一点,但我认为该陈述是错误的。怎么说包含空格,加1。 编辑: 我发现(感谢Blender)可以用更少的代码来做到这一点: 问题答案: 使用方括号,而不是括号: 或: 您也可以使用:

  • 问题内容: 我在做作业时遇到了这个问题(老实说,至少没有试图隐藏它),在解决该问题时遇到了问题。 给定以下声明:字符串短语=“ WazzUp?-谁在第一时间???-IDUNNO”;编写必要的代码以计算字符串中的元音数量,并将适当的消息打印到屏幕上。 这是我到目前为止的代码: 但是,当我运行它时,它只会产生一堆空白行。有人可以帮忙吗? 问题答案: 应该是。 给出的值,然后加1。就像您现在拥有的一样,

  • 本文向大家介绍使用MySQL计算字符串中的字符数,包括了使用MySQL计算字符串中的字符数的使用技巧和注意事项,需要的朋友参考一下 今天,我需要从一个表中获取一些数据,该表中另一个字符串中不止一个字符串出现。基本上,我需要从一个表中查找所有深度超过3级(即带有3个斜杠)的URL,但是意识到在MySQL中没有函数可以执行此操作。我找到了另一种方法,但它使我思考如何可能。 找到解决方案并不是很困难,我

  • 问题内容: 单词示例:a,akkka,akokaa,kokoko,kakao,ooooooa,kkako,kakaoa 我需要正则表达式女巫给带有2个或更少的’a’的单词,而不给没有’a’的单词 结果:a,akka,kakao,ooooooa,kkako 好的,实际上我正在使用: 这将返回0行,其中有带有2位和3位的单词 问题答案: 更新以在两者之间使用。

  • 本文向大家介绍使用js写一个计算字符串的字节数的方法相关面试题,主要包含被问及使用js写一个计算字符串的字节数的方法时的应答技巧和注意事项,需要的朋友参考一下 const computeLens = (s) => { return s.split('').length } console.log(computeLens('aaacvsa'));