package com.liuhuan.test;
import java.util.Scanner;
public class fun07 {
/*
* 題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int English=0,number=0,Space=0,others=0;
System.out.println("請輸入字符串:");
Scanner input=new Scanner(System.in);
String str=input.nextLine();
char[] Char_str=str.toCharArray();
for(int i=0;i<Char_str.length;i++){
char ch=Char_str[i];
if(('z'>=ch&&ch>='a')||('A'<=ch&&ch<='Z')){
English++;
}
else if(ch>='0'&&ch<='9'){
number++;
}
else if(ch==' '){
Space++;
}
else others++;
}
System.out.println("英文字母"+English+"\r空格="+Space+"\r數字="+number+"\r其它字符="+others);
}
}
Java 編程題目 第七題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.