这两天基本都满课,没有进行什么其他的学习,先整理一下第一个不会的程序题。
统计用户从键盘输入的字符串中所包含的字母、数字和其他字符的个数。
import java.util.Scanner;public class Main06{ public static void main(String[] args){
Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:");
// 获取键盘输入的一个字符串
String input = scanner.nextLine();
int countCharacter = 0;
int countNumber = 0; // 数字
int countOther = 0; // 其他字符
//字符串转为数组
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char ch = charArray[i]; // 当前单个字符 if ('A' <= ch && ch <= 'Z'||'a' <= ch && ch <= 'z') {
countCharacter++;
} else if ('0' <= ch && ch <= '9') { countNumber++;
} else {
countOther++;
}
}
System.out.println("字母有:" + countCharacter); System.out.println("数字有:" + countNumber); System.out.println("其他字符有:" + countOther); }
}
这种题目我之前就碰到过,一直不会做,这次好好记一下。第三次任务今天晚上也布置下来了,之后慢慢学习。