初學java:統計字串中各種字元個數
import java.util.Scanner;
public class Test {
public static void main(String[] args) {Scanner in=new Scanner(System.in);
System.out.print("請輸入字串:");
String s=in.nextLine();
int letter=0,blank=0,digit=0,other=0;
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if (c>='a'&c<='z'|c>='A'&c<='Z')
letter++;
else if(c==' ')
blank++;
else if(c>='0'&c<='9')
digit++;
else
other++;
}
System.out.println(s+"中:\n英文字母個數為:"+letter+"\n空格字元的個數為:"+blank+"\n數字字元的個數為:"+digit+"\n其他字元的個數為:"+other);
}
}