【Java】編寫一個程式,輸出一個字串中的大寫英文字母數,小寫英文字母數以及非英文字母數
阿新 • • 發佈:2019-01-27
/*
* 作者:筱筱
* 日期:20170507
* 功能:編寫一個程式,輸出一個字串中的大寫英文字母數,小寫英文字母數以及非英文字母數。
*/
public class Aa3Count {
public static void main(String[] args){
String st = "A123 C_Ddf8*gX";
int numCapital = 0;
int numlowercaseletter = 0;
int numOther = 0;
for(int i=0; i<st.length(); i++){
char c = st.charAt(i);
if('a'<=c && c<='z'){
numlowercaseletter++;
}else if('A'<=c && c<='Z'){
numCapital++;
}else{
numOther++;
}
}
System.out.println("大寫字母有:"+numCapital+" 小寫字母有:" +numlowercaseletter+" 非英文字母數有:"+numOther);
}
}
執行結果:
大寫字母有:4 小寫字母有:3 非英文字母數有:7