A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 爱吃柠檬 中级黑马   /  2016-8-30 23:10  /  2833 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

4.分析以下需求,并用代码实现:
        (1)从键盘循环录入录入一个字符串,输入"end"表示结束
        (2)将字符串中大写字母变成小写字母,小写字母变成大写字母,其它字符用"*"代替,并统计字母的个数
                举例:
                        键盘录入:Hello12345World
                        输出结果:hELLO*****wORLD
                                          总共10个字母

5 个回复

倒序浏览
楼主在么?我刚刚上论坛看到你这个题目,刚刚好我也在做.可能你现在已经毕业了,听说答题有黑马积分,写的可能不是很好.但是还是想答下.望楼主采纳
         * 分析:  1.创建while循环 判断如果键盘录入到end ,就跳出循环,否则循环一直进行
                                 *                 2.先将字符串转换成字符数组然后通过for循环遍历获取每一个字符
                                 *            4.然后把遍历的字符做判断是否是大小写还是数字,
                                 *           5.判断完之后用StringBuilder中的append方法进行添加
                                 *   6.全部添加完之后,调用toString方法进行打印.
                                 */
                                       
                                sc = new Scanner(System.in);
                                System.out.println("请输入Hello12345World:");
                                //接收字符串
                                while(true){
                                String line=sc.nextLine();
                                if(line.equals("end")){
                                        break;
                                }
                                StringBuilder sb=new StringBuilder();
                                char[] chr=line.toCharArray();
                                  for (int i = 0; i <chr.length; i++){
                                          //字符底部都是用转换成数字进行判断A是65 大Z是90
                                          if(chr[i]>='A' && chr[i]<='Z'){
                                          String str3=String.valueOf(chr[i]);//可以直接用类名调用因为是静态方法
                                          String str4=str3.toLowerCase();
                                          sb.append(str4);                  
                                          }         
                                          else if (chr[i]>='a' && chr[i]<='z'){
                                                                  String str=String.valueOf(chr[i]);
                                                                  String str2=str.toUpperCase();
                                                                  sb.append(str2);
                                                  
                                          }else{
                                                  sb.append("*");
                                          }
                                  }
                                  System.out.println(sb.toString());
                                       
                        }
                               
                               
                }       
                               
}                               
                               

回复 使用道具 举报
该题出现在day15的课后作业,学的是String类和StringBuffer类,还有什么解决方法吗
回复 使用道具 举报
还没学到这里啊!!!!
回复 使用道具 举报
class T {
        public static void main(String[] args) {
                String str = "";// 存储键盘录入值
                Scanner in = new Scanner(System.in);

                while (!str.equals("end")) {// 当输入end时结束循环
                        str = in.nextLine();// 获取录入值

                        // 将录入的字符串转化为字符数组,以便转化
                        char[] ch = str.toCharArray();
                        StringBuilder sb = new StringBuilder();// 存入转化好的数组

                        // 遍历数组,实现转化
                        for (char c : ch) {
                                String s = "";

                                if ('A' <= c && c <= 'Z')
                                        s = (c + "").toLowerCase();
                                if ('a' <= c && c <= 'z')
                                        s = (c + "").toUpperCase();
                                if ('0' <= c && c <= '9')
                                        s = "*";

                                sb.append(s);
                        }
                        System.out.println(sb.toString());
                }
               
                in.close();
        }
}

回复 使用道具 举报 1 0
class T {
        public static void main(String[] args) {
                String str = "";// 存储键盘录入值
                Scanner in = new Scanner(System.in);

                while (!str.equals("end")) {// 当输入end时结束循环
                        str = in.nextLine();// 获取录入值
                        StringBuilder sb = new StringBuilder(str);
                        for (int i = 0; i < sb.length(); i++) {
                                String s = sb.substring(i, i+1);
                               
                                if(s.matches("[A-Z]"))
                                        sb.replace(i, i+1, s.toLowerCase());
                                if(s.matches("[a-z]"))
                                        sb.replace(i, i+1, s.toUpperCase());
                                if(s.matches("[0-9]"))
                                        sb.replace(i, i+1, "*");       
                        }
                        System.out.println(sb.toString());
                }
               
                in.close();
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马