首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

一个小程序。求大大们帮帮忙。纠结了半天了。解决方案

2012-04-18 
一个小程序。求大大们帮帮忙。纠结了半天了。题目:编写一个程序,从键盘输入一串字符,统计这串字符中英文字母、

一个小程序。求大大们帮帮忙。纠结了半天了。
题目:编写一个程序,从键盘输入一串字符,统计这串字符中英文字母、数字、其他符号的字符数。
  import java.util.*;
import java.io.*;
public class test1{
public static void main(String args[]) throws Exception{
int num=0;
int cha=0;
int orth=0;
int i;
BufferedInputStream in=new BufferedInputStream(System.in);
while((i=in.read())!=-1){
if(i>=0&&i<=99){
num++;
}
else if(i>='a'&&i<='z'){
cha++;
}else{
orth++;
}
}
System.out.println(num+" "+cha+" "+orth);

}

输入不能停止啊.是不是我的循环结束条件错了。求解。

[解决办法]
好像是死循环了,我刚学JAVA,下面是我修改的(貌似也能统计你要的):
import java.util.*;
import java.io.*;
public class my1{
public static void main(String args[]){
int num=0;
int cha=0;
int orth=0;
Scanner cin = new Scanner (new BufferedInputStream(System.in));
while(true){
int i = cin.nextInt();
if(i>=0&&i<=99){
num++;
}
else if(i>='a'&&i<='z'){
cha++;
}
else if(i ==-1){

break;
}
else{
orth++;
}

}
System.out.println(num+" "+cha+" "+orth);

}
}
[解决办法]
只要有输入就不会等于-1

热点排行