我正在编写一个程序,它使用 Scanner 方法读取文本文件并输出:单词数、句子数、每句平均单词数、最长句子和最短句子。到目前为止,除了最长和最短的句子外,我什么都有,我似乎无法弄清楚。这是我到目前为止所拥有的...
import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class TestScanner {
public static void main(String args[])
{ Scanner in = new Scanner(System.in);
String x = in.next();
double count=0;
int nbSentences = 0;
while(in.hasNext())
{ String word = in.next();
nbSentences +=getNbSentences(word);
count++;
}
System.out.println("Number of Words: "+ count);
System.out.println("Number of Sentences: " + nbSentences);
System.out.println("Average Words In Sentence: " + (count/nbSentences));
System.out.println("Longest Sentence: ");
System.out.println("Shortest Sentence: ");
}
//**************************number of sentences*********************************
public static int getNbSentences(String word)
{ int result = 0;
char[] chars = word.toCharArray();
for(Character c : chars)
{ if(c == '.' || c == '!' || c == '?')
{ result++;
}
}
return result;
}
//*************************Longest Sentence*************************************
//This is where I'm stuck....
}
如果有人可以提供帮助,我将不胜感激!!