編寫一個程序,判斷輸出一個字符串中大寫英文字母數,和小寫英文字母數,和其他非英文字母數

 

import java.util.Scanner;

 

/**

* 編寫一個程序,判斷輸出一個字符串中大寫英文字母數,和小寫英文字母數,和其他非英文字母數

*

* @author arvin

*

*/

public class IfString {

public static void main(String[] args) {

int numUppercase = 0;// 大寫英文字母數

int numlowercase = 0;// 小寫英文字母數

int numOther = 0;// 其他非英文字母數

Scanner sc = new Scanner(System.in);

System.out.println("請輸入一個字符串:");

String getString = sc.next();

for (int i = 0; i < getString.length(); i++) {

char c = getString.charAt(i);//把每個元素摘出來

if (c >= 'a' && c <= 'z') {

numlowercase++;

} else if (c >= 'A' && c <= 'Z') {

numUppercase++;

} else {

numOther++;

}

 

}

System.out.println("大寫英文字母數:" + numUppercase + "小寫英文字母數:" + numlowercase + "非英文字母數:" + numOther);

sc.close();

 

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章