1. 程式人生 > >求字串中大小寫數字的個數

求字串中大小寫數字的個數

@求字串中大小寫數字的個數

    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 i = 0; i < s.length(); i++) {
    		char ch = s.charAt(i);
    		if(ch>='A'&&ch<='Z'){
    			bigcount++;
    			//System.out.println("大寫字母有:"+bigcount);
    		}else if(ch>='a'&&ch<='z'){
    			smallcount++;
    			//System.out.println("小寫 字母有:"+smallcount);
    		}else if(ch>='0'&&ch<='9'){
    			numbercount++;
    			//System.out.println("數字出現的個數:"+numbercount);
    		}else{
    			System.out.println("字元"+ch+"非法");
    		}
    	}
    	System.out.println("大寫字母有:"+bigcount+"小寫 字母有:"+smallcount+"數字出現的個數:"+numbercount);
    }