每天一個java小程序-7

 JAVA練習題,能做多少就做多少。http://bbs.csdn.net/topics/110067294


這個是從CSDN看到的。每天一個吧 。

【程序7】 
題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。 
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);
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章