import java.util.Scanner; public class Test2 { public static void main(String[] args) { //局部變量使用前一定要初始化 int lowCount = 0,upperCount=0,numCount=0,otherCount=0; Scanner sc=new Scanner(System.in); System.out.println("請輸入字符串:"); String str=sc.next(); char[] chars=str.toCharArray(); for (int i = 0; i < chars.length; i++) { if(97<=chars[i]&&chars[i]<=122){ //小寫字母 lowCount++; } else if(65<=chars[i]&&chars[i]<=90){ upperCount++; } else if(48<=chars[i]&&chars[i]<=57){ numCount++; } else{ otherCount++; } } System.out.println("大寫字母個數:"+upperCount); System.out.println("小寫字母個數:"+lowCount); System.out.println("數字個數:"+numCount); System.out.println("其他字符個數:"+otherCount); } }