诚挚请教以下程序结果
我输入一串字符却没有结果输出
按ctrl + c 退出时却有三个数字输出(但与输入不符)
代码如下:import java.io.*;
class WordCountDemo
{
public static int words, lines, chars;
public static void wc(InputStreamReader isr)throws IOException
{
int i;
boolean lastWhite = true;
String whiteSpace = "\t\n\r";
while ((i = isr.read()) != -1) {
chars++;
if(i == '\n')
lines++;
int index = whiteSpace.indexOf(i);
if(index == -1)
{
if(lastWhite)
words++;
lastWhite = false;
}
else
lastWhite = true;
if(chars != 0)
lines++;
}
}
public static void main(String a[])
{
FileReader fr;
try {
if(a.length == 0)
wc(new InputStreamReader(System.in));
else
{
for (int i = 0; i<a.length; i++) {
fr = new FileReader(a[i]);
wc(fr);
}
}
}
catch (Exception ex) {
return;
}
System.out.println (chars);
System.out.println (lines);
System.out.println (words);
}
}
我输入一串字符却没有结果输出
按ctrl + c 退出时却有三个数字输出(但与输入不符)
代码如下:import java.io.*;
class WordCountDemo
{
public static int words, lines, chars;
public static void wc(InputStreamReader isr)throws IOException
{
int i;
boolean lastWhite = true;
String whiteSpace = "\t\n\r";
while ((i = isr.read()) != -1) {
chars++;
if(i == '\n')
lines++;
int index = whiteSpace.indexOf(i);
if(index == -1)
{
if(lastWhite)
words++;
lastWhite = false;
}
else
lastWhite = true;
if(chars != 0)
lines++;
}
}
public static void main(String a[])
{
FileReader fr;
try {
if(a.length == 0)
wc(new InputStreamReader(System.in));
else
{
for (int i = 0; i<a.length; i++) {
fr = new FileReader(a[i]);
wc(fr);
}
}
}
catch (Exception ex) {
return;
}
System.out.println (chars);
System.out.println (lines);
System.out.println (words);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货