统计用户输入的多行文本中的单词和字符数。

huangapple go评论58阅读模式
英文:

Counting number of words and characters from several lines of user input

问题

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("请输入文本(输入DONE结束):");
    String text = "";
    while (!"DONE".equals(text)) {
        text = input.nextLine();
        if (!"DONE".equals(text)) {
            String[] words = text.split(" ");
            int wordCount = words.length;
            int charCount = 0;
            
            for (String word : words) {
                charCount += word.length();
            }
            
            System.out.println("单词数量:" + wordCount);
            System.out.println("非空格字符数量:" + charCount);
        }
    }
}

注意:以上代码为输入文本行数的方式,每次输入一行文本,输入DONE结束。在每一行输入结束后会输出该行的单词数量和非空格字符数量。

英文:

Description: "Write a program to read several lines of text (each line ending with a '.') from Scanner. It should count the number of words and the number of non-space characters. The program should stop accepting input when it reads the word (DONE) in a separate line of input."

I'm having trouble with this program. Whenever I input the sample text and type in DONE, the number of words and characters match that of only the word 'DONE'. If someone could provide a working fix, that would be very helpful towards my understanding of the problems at hand.

public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.print("Please enter text (type DONE to quit): ");
	String text = "";
	while (!"DONE".equals(text)) {
		text = input.next();
	}
	String[] numOfWords = text.split(" ");
	String[] numOfChars = text.split("");
	System.out.print("Number of words: " + numOfWords.length);
	System.out.println("");
	System.out.print("Number of non-space characters: " + numOfChars.length);
	}
}

答案1

得分: 2

由于上面的答案是错误的(无法编译或无法工作),您可以尝试这样做:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("请输入文本(输入DONE以退出):");
    String text = "";
    String output = "";
    while (!"DONE".equals(text)) {
        text = input.next();
        output += text.replace("DONE", "") + " ";
    }
    String[] numOfWords = output.split(" ");
    String[] numOfChars = output.split("");
    System.out.print("单词数量:" + numOfWords.length);
    System.out.println();
    System.out.print("非空格字符数量:" + numOfChars.length);
}

有效地为输入字符串创建另一个存储,一旦提供了关键字“DONE”,则在删除单词“DONE”后退出循环。

英文:

As the answer above is incorrect (doesnt compile or work) you could try this:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter text (type DONE to quit): ");
    String text = "";
    String output = "";
    while (!"DONE".equals(text)) {
        text = input.next();
        output += text.replace("DONE", "") + " ";
    }
    String[] numOfWords = output.split(" ");
    String[] numOfChars = output.split("");
    System.out.print("Number of words: " + numOfWords.length);
    System.out.println("");
    System.out.print("Number of non-space characters: " + numOfChars.length);
}

Effectively creating another storage for the input string and once the DONE keyword is supplied exit the loop after removing the word "DONE".

答案2

得分: 1

在你的代码中,发生的情况是最后你提供的输入是“DONE”,因此只计算“DONE”。请使用以下代码:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("请输入文本(输入DONE退出):");
    String text = "";
    while (true) {
        if (!text.equals("DONE")) {
            break;
        }
        text += " " + input.nextLine();
    }
    String[] numOfWords = text.split(" ");
    String[] numOfChars = text.split("");
    System.out.print("单词数量:" + numOfWords.length);
    System.out.println();
    System.out.print("非空格字符数量:" + numOfChars.length);
}
英文:

In your code what happens is that at last you give the input is DONE so it counts only done use the following code

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter text (type DONE to quit): ");
    String text = "";
    while (true) {
        if(!text.equals("DONE")){
                 break;
         }
        text += " " + input.nextLine();
         
    }
    String[] numOfWords = text.split(" ");
    String[] numOfChars = text.split("");
    System.out.print("Number of words: " + numOfWords.length);
    System.out.println("");
    System.out.print("Number of non-space characters: " + 
     numOfChars.length);
}

}

答案3

得分: 1

其他两个答案都未能修复计算字符数错误。以下代码正确处理了缺少空格的情况,去除了输入终止文本,并正确计算了非空格字符的数量:

```java
import java.util.*;
public class MyClass {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入文本(输入 DONE 退出):");
        String text = "";
        String output = "";
        while (!"DONE".equals(text)) {
            text = input.next();
            output += text.replace("DONE", "") + " ";
        }
        String[] numOfWords = output.split(" ");
        int numOfChars = output.replace(" ", "").length();
        System.out.print("单词数:" + numOfWords.length);
        System.out.println("");
        System.out.print("非空格字符数:" + numOfChars);
    }
}

输入带有四个单词的文本 DONE

输出

请输入文本(输入 DONE 退出):

单词数:4

非空格字符数:17


<details>
<summary>英文:</summary>

Both of the other answers fail to fix the error in counting the number of characters. The following code correctly accounts for lack of spaces, gets rid of the input termination text, and correctly counts the non-space characters:

import java.util.*;
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter text (type DONE to quit): ");
String text = "";
String output = "";
while (!"DONE".equals(text)) {
text = input.next();
output += text.replace("DONE", "") + " ";
}
String[] numOfWords = output.split(" ");
int numOfChars = output.replace(" ", "").length();
System.out.print("Number of words: " + numOfWords.length);
System.out.println("");
System.out.print("Number of non-space characters: " + numOfChars);
}
}

**Input**: *text with four words DONE*

**Output**


Please enter text (type DONE to quit): 

Number of words: 4

Number of non-space characters: 17

</details>



huangapple
  • 本文由 发表于 2020年10月12日 23:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64320512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定