Character

Character 类在对象中包装一个基本类型 char 的值
此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然

构造方法:
Character(char value)

方法:

1  * public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
2  * public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
3  * public static boolean isDigit(char ch):判断给定的字符是否是数字字符
4 
5  * public static char toUpperCase(char ch):把给定的字符转换为大写字符
6  * public static char toLowerCase(char ch):把给定的字符转换为小写字符
 1 /*
 2  * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
 3  * 
 4  * 分析:
 5  *         A:定义三个统计变量。
 6  *             int bigCont=0;
 7  *             int smalCount=0;
 8  *             int numberCount=0;
 9  *         B:键盘录入一个字符串。
10  *         C:把字符串转换为字符数组。
11  *         D:遍历字符数组获取到每一个字符
12  *         E:判断该字符是
13  *             大写    bigCount++;
14  *             小写    smalCount++;
15  *             数字    numberCount++;
16  *         F:输出结果即可
17  */
18 public class CharacterTest {
19     public static void main(String[] args) {
20         // 定义三个统计变量。
21         int bigCount = 0;
22         int smallCount = 0;
23         int numberCount = 0;
24 
25         // 键盘录入一个字符串。
26         Scanner sc = new Scanner(System.in);
27         System.out.println("请输入一个字符串:");
28         String line = sc.nextLine();
29 
30         // 把字符串转换为字符数组。
31         char[] chs = line.toCharArray();
32 
33         // 历字符数组获取到每一个字符
34         for (int x = 0; x < chs.length; x++) {
35             char ch = chs[x];
36 
37             // 判断该字符
38             if (Character.isUpperCase(ch)) {
39                 bigCount++;
40             } else if (Character.isLowerCase(ch)) {
41                 smallCount++;
42             } else if (Character.isDigit(ch)) {
43                 numberCount++;
44             }
45         }
46 
47         // 输出结果即可
48         System.out.println("大写字母:" + bigCount + "个");
49         System.out.println("小写字母:" + smallCount + "个");
50         System.out.println("数字字符:" + numberCount + "个");
51     }
52 }

 

posted @ 2015-09-05 09:22  yweihainan  阅读(425)  评论(0编辑  收藏  举报