我有两天的作业,而且我已经尝试了很多天来做到这一点,但是我很沮丧,试图回到工作上,仍然没有进展。
分配如下:
Java程序,用于从中计算上述统计信息 任何文本文件。实际情况如下: 输入文件的名称:example.txt 1个字母的单词所占比例:3.91%(74个单词) 2个字母的单词所占比例:18.52%(349个单词) 3个字母的单词所占的比例:24.24%(456个单词) 4个字母的单词所占比例:19.80%(374个单词) 5个字母的单词所占比例:11.33%(212个单词) … … 12个字母的单词所占比例:0.45%(8个单词) 13个(或更多)字母单词的比例:0.51%(9个单词)
现在,为了执行此操作,我想将程序分为三种方法:阅读该方法,对字母进行计数并加以区分,最后将其显示为上面的示例。现在我已经说了,现在是我的代码:
/*like make smaller functions
where each function has one task
like to loop through the file and return an array of words
then use that as input to another function whose purpose is to count the
letters
and then pass that array into a function for printing that.
*/
import java.io.*;
import java.util.Scanner;
class Autorship {
public static void main(String[] args) {
try {
System.out.println("Name of input file: ");
Scanner sc1 = new Scanner(System. in );
sc1.useDelimiter("[^a-zA-Z]");
String fname = sc1.nextLine();
sc1.close();
sc1 = new Scanner(new FileReader(fname));
sc1.useDelimiter("[^a-zA-Z]");
String line;
System.out.println(WordCount(fname, sc1));
} catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
public static int WordCount(String fname, Scanner sc1) {
int wordCount = 0;
int lineCount = 0;
while (sc1.hasNextLine()) {
String line;
line = sc1.nextLine();
lineCount++;
String[] strings = line.split(" ");
int[] counts = new int[14];
for (String str: strings)
if (str.length() < counts.length) counts[str.length()] += 1;
System.out.println("This is counts length: " + counts.length);
for (int i = 1; i < counts.length; i++)
System.out.println(i + " letter words: " + counts[i]);
}
return 0;
}
}
现在请我不想要答案,因为那是窃,我不是那种人,我只需要一点帮助就可以继续进步,我是如此现在卡住了,谢谢^^
答案 0 :(得分:0)
这里是经过调整的有效版本。我评论了我编辑的行。
您的代码还不错,并且运行良好。您遇到的唯一问题是,您已在while循环中打印出了字母计数,而不是在循环中打印了出来。因此,从文件中读取的每一行都重复执行此操作。
请注意:我强烈建议始终使用大括号,即使Java语法允许仅在要执行的一行代码之后将它们不与if语句和for循环一起使用。但是不使用它们会使代码更难阅读且容易出错。
public static void main(String[] args) {
try {
System.out.println("Name of input file: ");
Scanner sc1 = new Scanner(System. in );
sc1.useDelimiter("[^a-zA-Z]");
String fname = sc1.nextLine();
sc1.close();
sc1 = new Scanner(new FileReader(fname));
sc1.useDelimiter("[^a-zA-Z]");
String line;
System.out.println("WordCount: " + WordCount(fname, sc1)); // edited
} catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
public static int WordCount(String fname, Scanner sc1) {
int wordCount = 0;
int lineCount = 0;
final int MAXIMUM_LENGTH = 14; // edited. Better use a constant here.
int[] counts = new int[MAXIMUM_LENGTH]; // edited. Constant applied
while (sc1.hasNextLine()) {
String line = sc1.nextLine();
// increment line count
lineCount++;
String[] strings = line.split(" ");
// increment word count
wordCount += strings.length; // added
// edited. curly brackets and constant MAXIMUM_LENGTH
for (String str: strings) {
if (str.length() < MAXIMUM_LENGTH) {
counts[str.length()] += 1;
}
}
}
// edited / added. finally show the results
System.out.println("maximum length: " + MAXIMUM_LENGTH);
System.out.println("line count: " + lineCount);
System.out.println("word count: " + wordCount);
// edited. moved out of the while-loop. MAXIMUM_LENGTH applied.
for (int i = 1; i < MAXIMUM_LENGTH; i++) {
System.out.println(i + " letter words: " + counts[i]);
}
// edited.
return wordCount;
}