输出一个字符串里面的空格,字母还有数字的数目:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
public class Test {
public static void main(String[] args) throws IndexOutOfBoundsException {
String a = "I am a student whose age is 20";
shumu(a); //输出字符串空格和字母还有数字的数目。
}
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自带的函数,意思是对于某一个字符串,取出某个位置的字符。@马克-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("数字的个数为: " + shuzi);
System.out.println("字母的个数为: " + zimu);
System.out.println("空格的数目为: " + kongge);
}
}