分析:
1.鍵盤錄入一個字符串數據
2.定義三個統計變量,初始化值都是0
3.遍歷字符串,得到每個字符
4.拿字符進行比較,如何判斷大小寫?假設ch是一個字符:
大寫判斷:ch>='A' && ch <= 'Z';
小寫判斷:ch >='a' && ch <='z';
數字判斷:ch >='0' && ch <='9';
5.輸出統計結果
Code:
Public static void main (String [] args ){
Scanner sc = new Scanner (System.in);
System.out.println("請輸入一個字符:");
String s = sc.nextLine();
int bigCount = 0;
int smallCount = 0;
int numberCount =0;
for (int x=0;x<s.length();x++){
char ch = s.charAt(x);
if (ch >= 'A' && ch <= 'Z'){
bigCount++;
}else if (ch >= ' a' && ch <= 'z'){
smallCount++;
}else if (ch >= '0' && ch <= '9'){
numberCount++;
}else{
System.out.println("字符"+ch+"非法');
System.out.println("大寫"+bigCount+"個');
System.out.println("小寫"+smallCount+"個');
System.out.println("數字"+numberCount+"個");
}
}
}
}
}