輸出一個字符串里面的空格,字母還有數(shù)字的數(shù)目:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
public class Test {
public static void main(String[] args) throws IndexOutOfBoundsException {
String a = "I am a student whose age is 20";
shumu(a); //輸出字符串空格和字母還有數(shù)字的數(shù)目。
}
static void shumu(String a) {
int shuzi = 0, zimu = 0, kongge = 0;
char[] c = new char[a.length()];
for (int i = 0; i < a.length(); i++) {
/*下面這句話是java自帶的函數(shù),意思是對于某一個字符串,取出某個位置的字符。@馬克-to-win*/
c[i] = (char) a.codePointAt(i);
if (c[i] >= '0' && c[i] <= '9') {
shuzi++;
}
if ((c[i] >= 'A' && c[i] <= 'Z') || (c[i] >= 'a' && c[i] <= 'z')) {
zimu++;
}
if (c[i] == ' ') {
kongge++;
}
}
System.out.println("數(shù)字的個數(shù)為: " + shuzi);
System.out.println("字母的個數(shù)為: " + zimu);
System.out.println("空格的數(shù)目為: " + kongge);
}
}