JAVA练习题,能做多少就做多少。http://bbs.youkuaiyun.com/topics/110067294
这个是从优快云看到的。每天一个吧 。
【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
偷工减料不做中文判断了。。。。
==== Main.java ====
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
byte[] bytes = null;
int en = 0, num = 0, space = 0, others = 0;
int i;
String a = "";
System.out.println("请输入A:");
try {
a = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
bytes = a.getBytes();
for (i = 0; i < bytes.length; i++) {
char b = (char) bytes[i];
if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) {
en++;
} else if (b >= '0' && b <= '9') {
num++;
} else if (b == ' ') {
space++;
} else {
others++;
}
}
System.out.println("En is " + en);
System.out.println("Num is " + num);
System.out.println("Space is " + space);
System.out.println("Others is " + others);
}
}