import java.io.*;public class tt1
{
public static void main(String args[])
{
char c;
int l=0,m=0,n=0;
try
{
c=(char)System.in.read();
while (c !=13 && c !=10)
{
if(c>='A' && c<='Z' || c>='a' && c<='z')
l++;
else
if(c>='0' && c<='9')
m++;
else
n++;
c=(char)System.in.read();
}
} catch (IOException e){} System.out.println(l);
System.out.println(m);
System.out.println(n);
}
}//这样就可以了
{
public static void main(String args[])
{
char c;
int l=0,m=0,n=0;
try
{
c=(char)System.in.read();
while (c !=13 && c !=10)
{
if(c>='A' && c<='Z' || c>='a' && c<='z')
l++;
else
if(c>='0' && c<='9')
m++;
else
n++;
c=(char)System.in.read();
}
} catch (IOException e){} System.out.println(l);
System.out.println(m);
System.out.println(n);
}
}//这样就可以了
while (c !='\n')读入数据在外面陷入了死循环!只要能读入一个字符。
而且程序写的很乱。变量定义模糊,没有缩进层次很乱。
要改改。
{
public static void main(String args[])
{
char c;
int alphas=0,numbers=0,others=0;
System.out.println("please input a sentence to calculate !"); try
{
while((c=(char)System.in.read()) !='\n')
{
if(c>='A' && c<='Z' || c>='a' && c<='z')
alphas++;
else
if(c>='0' && c<='9')
numbers++;
else
others++;
}
} catch (IOException e){} System.out.println("In this sentence hava "+alphas+" aphas");
System.out.println("In this sentence hava "+numbers+" numbers");
System.out.println("In this sentence hava "+others+" other characters");
}
}这样应该好些了。
请你看看。
希望对你有帮助!
这是不妥当的。回车应该是结束标志!
char[] c=str.toCharArray();
int charLen=0,otherLen=0,numLen=0;
for(int i=0;i<c.length;i++){
if(c[i]==13)
break;//如果回车,那么结速统计
if((c[i]>='A'&&c[i]<='Z')||(c[i]>='a'||c[i]<='z')
charLen++;
else if(数字)
numLen++;
else otherLen++;
}
System.out.println(.....);//略.