这是我写的统计一个输入字符串的程序, 但我不知道怎么调用输入字符串函数?哪位高手告诉我一下,谢谢。 现在我只能输入单个字符。 程序://输入字符串中字符中字母,数字和其他字符数目。class e2{
public static void main(String args[])
throws java.io.IOException{
int count_char=0;
int count_number=0;
int count_other=0;
String s=" ";
char c=' ';
System.out.print("Please input the String numbers:");
c=(char) System.in.read(); //就是这行,怎么输入字符串?? for(int i=0;i<s.length();i++){ //从字符串中逐个获取字符
if(c>='a'&&c<='z'||c>='A'&&c<='Z') count_char++; //统计字母个数
else if(c>='0'&&c<='9') count_number++; //统计数字个数
else count_other++;
}
System.out.println("count_char="+count_char);
System.out.println("count_number"+count_number);
System.out.println("count_other"+count_other);
}
}
请大家指点,我是初学java的,谢谢大家。
public static void main(String args[])
throws java.io.IOException{
int count_char=0;
int count_number=0;
int count_other=0;
String s=" ";
char c=' ';
System.out.print("Please input the String numbers:");
c=(char) System.in.read(); //就是这行,怎么输入字符串?? for(int i=0;i<s.length();i++){ //从字符串中逐个获取字符
if(c>='a'&&c<='z'||c>='A'&&c<='Z') count_char++; //统计字母个数
else if(c>='0'&&c<='9') count_number++; //统计数字个数
else count_other++;
}
System.out.println("count_char="+count_char);
System.out.println("count_number"+count_number);
System.out.println("count_other"+count_other);
}
}
请大家指点,我是初学java的,谢谢大家。
class e2{
public static void main(String args[])
throws java.io.IOException{
int count_char=0;
int count_number=0;
int count_other=0;
String s=" ";
char c=' ';
System.out.print("Please input the String numbers:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine(); //从键盘输入字符 ******************
for(int i=1;i<s.length();i++){ //从字符串中逐个获取字符
if(c>='a'&&c<='z') count_char++; //统计字母个数||(c>='A'&&c<='Z')
else if(c>='0'&&c<='9') count_number++; //统计数字个数
else count_other++;
}
System.out.println("count_char="+count_char);
System.out.println("count_number="+count_number);
System.out.println("count_other="+count_other);
}
}
c = s.toCharArray();
for(int i=1;i<c.length;i++){ //从字符串中逐个获取字符
if(c[i]>='a'&&c[i]<='z') count_char++; //统计字母个数||(c>='A'&&c<='Z')
else if(c[i]>='0'&&c[i]<='9') count_number++; //统计数字个数
else count_other++;
}
import java.io.*;
class e2{
public static void main(String args[])
throws java.io.IOException{
int count_char=0;
int count_number=0;
int count_other=0;
String s=" ";
char c=' ';
System.out.print("Please input the String numbers:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine(); //从键盘输入字符
c=s.toCharArray(); //?????????????????
for(int i=1;i<s.length();i++){ //从字符串中逐个获取字符
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) count_char++; //统计字母个数
else if(c>='0'&&c<='9') count_number++; //统计数字个数
else count_other++;
}
System.out.println("count_char="+count_char);
System.out.println("count_number="+count_number);
System.out.println("count_other="+count_other);
}
}请问c=s.toCharArray(); 怎么错了?
import java.io.BufferedReader;
import java.io.InputStreamReader;class e2 {
public static void main(String args[]) throws java.io.IOException {
int count_char = 0;
int count_number = 0;
int count_other = 0;
String s = " ";
char c = ' ';
char[] cs = null;
System.out.print("Please input the String numbers:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); // 从键盘输入字符 cs = s.toCharArray(); // ????????????????? for (int i = 1; i < cs.length ; i++) { // 从字符串中逐个获取字符
c = cs[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
count_char++; // 统计字母个数 else if (c >= '0' && c <= '9')
count_number++; // 统计数字个数 else
count_other++;
}
System.out.println("count_char=" + count_char);
System.out.println("count_number=" + count_number);
System.out.println("count_other=" + count_other);
}
}