chapter13-homework-Java

题目1

(1) 将字符串中指定部分进行反转。比如将 “abcdef” 反转为 “aedcbf”
(2) 编写方法 public static String reverse(String str, int start , int end) 搞定

思路分析
(1)先确定方法定义
(2)把 String 转成 char[] ,这样才能修改数据
(3)先写出正确的情况,再取反

知识点
1、String不可更改,char[]可以
2、String的构造方法,可以直接传入char[]
3、String和char[]的互相转换
String->char
char->String

public class Homework01 {
    public static void main(String[] args) {
        String str = "abcdef";
        System.out.println("=====交换前=====");
        System.out.println(str);

        try {
            str = reverse(str, 0, 0);
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("=====交换后=====");
        System.out.println(str);
    }
    public static String reverse(String str, int start, int end) {
        //先写正确情况,然后取反
        if(!(str != null && start >=0 && end >start && end < str.length())) {
            throw new RuntimeException("参数不正确");
        }
        char[] chars = str.toCharArray();
        char temp = ' ';
        for(int i = start, j = end; i < j; i++, j--) {
            temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }
        return new String(chars);
    }
}

题目2

输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象
要求:
(1) 用户名长度为2或3或4
(2) 密码的长度为6,要求全是数字isDigital
(3)邮箱中包含@和.并且@在.的前面

思路分析
(1) 先编写方法 userRegister(String name,String pwd,String email) {}
(2) 针对输入的内容进行校核,如果发现有问题,就抛出异常,给出提示
(3) 单独的写一个方法,判断密码是否全部是数字字符bool
(4) 先写出正确的情况,再取反

public class Homework02 {
    public static void main(String[] args) {
        String name = "jack";
        String pwd = "123332";
        String email = "390258@qq.com";

        try {
            userRegister(name,pwd,email);
            System.out.println("恭喜你注册成功");
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public static void userRegister(String name,String pwd,String email) {
   		if(!(name != null && pwd != null && email != null)){
            throw new RuntimeException("不为空");
        }

        //用户名长度
        int ulen = name.length();
        if(!(ulen >=2 && ulen <= 4)) {
            throw new RuntimeException("用户名字长度为2或3或4");
        }

        //密码的长度
        if(!(pwd.length() == 6 && isDigital(pwd))) {
            throw new RuntimeException("用户名字长度为2或3或4");
        }

        //邮箱
        int i = email.indexOf('@');
        int j = email.indexOf('.');
        if(!(i>0 && j>i)) {
            throw new RuntimeException("邮箱中包含@和.并且@在.的前面");
        }

    }
    public static boolean isDigital(String str) {
        char[] chars = str.toCharArray();
        for(int i = 0; i < chars.length; i++) {
            if(chars[i] < '0' || chars[i] > '9') {
                return false;
            }
        }
        return true;
    }
}

题目3

(1) 编写 java程序,输入形式为:Han Shun Ping的人名,以Ping,Han .S的形式打印出来。其中.S是中间单词的首字母。
(2) 例如输入“Willian Jefferson Clinton”,输出形式为:Clinton,Willian .J

思路分析
(1) 对输入的字符串进行分割 split(" ")
(2) 对得到的String[]进行格式化String.format
(3) 对输入的字符串进行校验即可

public class Homework03 {
    public static void main(String[] args) {

    }
    public void printName(String str) {
        if(str == null) {
            System.out.println("str不能为空");
            return;
        }
        String[] names = str.split(" ");
        if(names.length != 3) {
            System.out.println("输入的字符串格式不对");
            return;
        }

        String format = String.format("%s,%s .%c",names[2],names[0],names[1].toUpperCase().charAt(0));
        System.out.println(format);
    }
}

在这里插入图片描述

题目4

4.编程题Homework04.java
输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字

思路分析:
(1)遍历字符串,如果char在’0’~'9’就是一个数字
(2)如果char在 ‘a’~‘z’就是一个小写字母
(3)如果char在’A’~'Z’就是一个大写字母
(4)使用三个变量来记录统计结果

知识点:
charAt方法
在这里插入图片描述

public class Homework04 {
    public static void main(String[] args) {
        String str = "abcHHHH U 1234";
        countStr(str);
    }

    public static void countStr(String str) {
        if (str == null) {
            System.out.println("输入不能为 null");
            return;
        }
        int strLen = str.length();
        int numCount = 0;
        int lowerCount = 0;
        int upperCount = 0;
        int otherCount = 0;
        for (int i = 0; i < strLen; i++) {
            if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                numCount++;
            } else if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
                lowerCount++;
            } else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                upperCount++;
            } else {
                otherCount++;
            }
        }

        System.out.println("数字有 " + numCount);
        System.out.println("小写字母有 " + lowerCount);
        System.out.println("大写字母有 " + upperCount);
        System.out.println("其他字符有 " + otherCount);
    }
}

题目5

判断输出

public class Homework05 {
    public static void main(String[] args) {
        String s1 = "hspedu";
        Animal a = new Animal(s1);
        Animal b = new Animal(s1);
        System.out.println(a == b);
        System.out.println(a.equals(b));
        System.out.println(a.name == b.name);
        String s4 = new String("hspedu");
        String s5 = "hspedu";

        System.out.println(s1 == s4);
        System.out.println(s4 == s5);

        String t1 = "hello" + s1;
        String t2 = "hellohspedu";
        System.out.println(t1.intern() == t2);
    }
}

class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }
}

① FALSE 引用类型比地址
② FALSE 没重写,默认==,同上,引用类型比地址
③ TRUE 两个name一样
④ FALSE s1直接指向方法区的常量池,s4指向堆,堆中的name再指向常量区 这是String,里面的属性是 final char[] value,所以是value指向常量池。
⑤ FALSE 同上
⑥ A) String t1 = “hello” + s1; 在字符串拼接的时候,都是常量会有优化;如果里面有变量,底层是做了一个 StringBuilder append方法。
B) t1是先创建了一个StringBuilder,append先加入“hello”,再加入s1对应的字符串内容加进去。然后再创建一个堆里面的String返回。
C) t2 是直接赋值常量值,所以直接指向常量池。
D) intern就是返回字符串在常量池的地址
答案是TRUE

在这里插入图片描述

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值