1. 程式人生 > >筆試題——編寫java程式:輸入一個字串,判斷有幾個英文字母,有幾個數字,有幾個其它的字元

筆試題——編寫java程式:輸入一個字串,判斷有幾個英文字母,有幾個數字,有幾個其它的字元

public static void main(String[] args) {
	
	int count_abc=0,count_num=0,count_oth=0;
	//輸入一串數
	Scanner scan=new Scanner(System.in);
	String str = scan.next();
	char[] chars = str.toCharArray();
	//判斷每個字元
	for(int i = 0;i<chars.length;i++){
		if((chars[i]>=65&&chars[i]<=90)||(chars[i]>=97&&chars[i]<=122)){
			count_abc++;
		}else if(chars[i]>=48&&chars[i]<=57){
			count_
num
++; }else{ count_oth++; } } System.out.println("字母有:"+count_abc+"個"); System.out.println("數字有:"+count_num+"個"); System.out.println("其他的有:"+count_oth+"個"); } public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String getMess=scan.nextLine();

int zm=0;
int num=0;
int other=0;
char [] ch = getMess.toCharArray();
for(int i=0;i<ch.length;i++){
if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z')){
zm=zm+1;
}else if(ch[i]>47 && ch[i]<58){
num=num+1;
}else{
other=other+1;
}
}
System.out.println("字母:"+zm);
System.out.println("數字:"+num);

System.out.println("其它:"+other);
}