public class Static{private String input;
private int bLetter =0;private int sLetter =0;private int numbers =0;private int others =0;private int spaces =0;
public Static(){? this.getString();? char[] list = charsOfString(input);
for (int i=0;i<list.length ;i++ )? { int flag = letterToCode(list[i]);if(flag>=65 && flag<=90) //註意:這裏是大寫字母的ASCII碼值範圍,下面壹樣 { bLetter += 1 ; } else if (flag>=97 && flag<=122) { sLetter += 1; } else if (flag>=48 && flag<=57) { numbers += 1; } else if (flag == 32) { spaces += 1; } else { others += 1; }? }
System.out.println("您輸入的字符串中的大寫字母有:"+bLetter);? System.out.println("您輸入的字符串中的小寫字母有:"+sLetter);? System.out.println("您輸入的字符串中的數字有:"+numbers);? System.out.println("您輸入的字符串中的空格有:"+spaces);? System.out.println("您輸入的字符串中的其他字符有:"+others);}
public static void main(String args[]){? new Static();}
/*** 該方法獲取壹組字符串*/public void getString(){? BufferedReader in = new BufferedReader(? new InputStreamReader(System.in));? System.out.println("請輸入壹串字符串:");
try? { this.input = in.readLine();? }? catch (Exception e)? { System.out.println(e);? }} /*** 該方法返貨該字符串的各個字符到壹個數組裏* 這裏我打算用ArrayList的,但是他只能保存為Object類型* 就是不知道怎麽把Object轉換為char,所以,這裏就用char[]*/public char[] charsOfString(String s){? char[] list = new char[s.length()]; for (int i=0 ;i<s.length() ;i++ )? { list[i] = s.charAt(i);? } return list;} /*** 該方法獲將字母轉換為對應ASCII碼*/public int letterToCode(char ch){? int s = ch;? return s; }}妳試試看 我也參考別人的