本帖最后由 王宝康 于 2012-7-12 17:39 编辑
- package string;
- public class StringDemo2 {
- /**
- * @param args
- * 获取所给字符串中大小写字母分别的个数
- * 思路:定义变量作为累加器,先把字符串转换成字符数组数组,然后遍历数组,对数组元素进行'a'-'z','A'-'Z'
- * 的范围判断。
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String str = "AsdWsaJswO";
- char [] arr = str.toCharArray();
- int lowerElement = 0;
- int upperElement = 0;
- int []arr1 = new int [str.length()];
- int lowerIndex = -1;
- int upperIndex = -1;
- for (int i = 0; i < arr.length; i++) {
- if (arr[i]>='a'&&arr[i]<='z') {
- lowerElement++;
- for (int j = 0; j < arr1.length; j++) {
- arr1[j] = arr[i];
- }
- }
- if (arr[i]>='A'&& arr[i]<='Z') {
- upperElement++;
- }
- }
- System.out.println("字符元素为:");
- for (int i = 0; i < arr.length; i++) {
- if (i!=arr.length-1) {
- System.out.print(arr[i]+",");
- }
- if (i==arr.length-1) {
- System.out.println(arr[i]);
- }
- }
- System.out.println("大写字母的个数为:"+upperElement+"...."+"小写字母的个数为:"+lowerElement);
- System.out.println("小写字母为:");
- for (int i = 0; i < arr1.length; i++) {
- if (i!=arr1.length-1) {
- System.out.print(arr1[i]+",");
- }
- if (i==arr1.length-1) {
- System.out.println(arr1[i]); //为啥木有打印小写字母
- }
- }
- }
- }
- 这是看毕老师的视频看来的一个小程序,我做了适当的修改,就是想打印出大写或者小写字母,思路是定义一个数组存储符合条件的大写或者小写字母,最后打印输出,可是却没有打印出预想结果...
复制代码 |
|