【发布时间】:2015-07-31 22:45:46
【问题描述】:
我对 java 比较陌生,我正在处理的程序有问题。该程序的主要思想是给定一个文档,计算文档中的总词数、每行的平均词数和每行的元音。 我想出了一个起点,但真的不知道如何继续。
import java.io.*;
public class Example {
public static void main(String[] args) throws IOException {
int vowelCount = 0, wordsAvg = 0, wordsCount = 0, a;
char ch;
String line;
int vowels1 = 0, vowels2 = 0, vowels3 = 0, vowels4 = 0, vowels5 = 0, vowels6 = 0, vowels7 = 0, vowels8 = 0, vowels9 = 0, vowels10 = 0;
BufferedReader in ;
in = new BufferedReader(new FileReader("message.txt"));
line = in .readLine();
}
for (int i = 0; i < line.length(); i++) {
ch = line.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels1++;
}
{
System.out.println(line);
System.out.println("--------------------------------------------");
System.out.println("Total number of words: " + wordsCount);
System.out.println("Average number of words per line: " + wordsAvg);
System.out.println("The number of vowels in line 1 is: " + vowels1);
System.out.println("The number of vowels in line 2 is: " + vowels2);
System.out.println("The number of vowels in line 3 is: " + vowels3);
System.out.println("The number of vowels in line 4 is: " + vowels4);
System.out.println("The number of vowels in line 5 is: " + vowels5);
System.out.println("The number of vowels in line 6 is: " + vowels6);
System.out.println("The number of vowels in line 7 is: " + vowels7);
System.out.println("The number of vowels in line 8 is: " + vowels8);
System.out.println("The number of vowels in line 9 is: " + vowels9);
System.out.println("The number of vowels in line 10 is: " + vowels10);
line = in .readLine();
}
}
到目前为止,我有程序读取文件并计算第一行的元音,但我真的不知道如何循环遍历另一行。 我还需要显示输出,因为我已经预先设置了它。 感谢您提供任何帮助,尽量使其尽可能简单。
【问题讨论】:
标签: java string algorithm io bufferedreader