Java 第13章 常用类 本章作业

字符串翻转

在这里插入图片描述
配合异常进行更加完善的代码编写

public class Homework01 {
    public static void main(String[] args) {
        String str = null; //"abcdef";
        int start = 1, end = 4;
        try {
            System.out.println(reverse(str, start, end));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String reverse(String str, int start, int end) {
        //对输入的参数做一个验证,之后
        // (1)写出正确的情况
        // (2) 然后取反即可
        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;
        }
        //使用chars重新构建一个String返回即可
        return new String(chars);
    }
}

注册处理

在这里插入图片描述

当不符合要求时,throw new RuntimeException(message) 可以创建信息不同的运行异常,只要在catch处接住即可,不用非要自己去创建什么异常类……

public class Homework02 {
    public static void main(String[] args) {
        String name = "abc";
        String pwd = "123456";
        String email = "ti@i@sohu.com";
        try {
            userRegister(name,pwd,email);
            System.out.println("恭喜你,注册成功~");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    /**
     * 思路分析
     * (1) 先编写方法 userRegister(String name, String pwd, String email) {}
     * (2) 针对 输入的内容进行校核,如果发现有问题,就抛出异常,给出提示
     * (3) 单独的写一个方法,判断 密码是否全部是数字字符 boolean
     */
    public static void userRegister(String name, String pwd, String email) {

        //再加入一些校验
        if(!(name != null && pwd != null && email != null)) {
            throw  new RuntimeException("参数不能为null");
        }

        //过关
        //第一关
        int userLength = name.length();
        if (!(userLength >= 2 && userLength <= 4)) {
            throw new RuntimeException("用户名长度为2或3或4");
        }
        //第二关
        if (!(pwd.length() == 6 && isDigital(pwd))) {
            throw new RuntimeException("密码的长度为6,要求全是数字");
        }
        //第三关
        int i = email.indexOf('@');
        int j = email.indexOf('.');
        if (!(i > 0 && j > i)) {
            throw new RuntimeException("邮箱中包含@和.   并且@在.的前面");
        }
    }
    //单独的写一个方法,判断 密码是否全部是数字字符 boolean
    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;
    }
}

字符串改写

在这里插入图片描述

  • 用空格分割String:String[] names = str.split(" ");

  • String.format()可以格式化输出内容。

public class Homework03 {
    public static void main(String[] args) {
        String name = "Willian Jefferson Clinton";
        printName(name);
    }
    public static 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);
    }
}

判断输出结果(内存布局)

在这里插入图片描述
结果:f f t f f t
注意点:

  1. String常量 + String变量最终存放在StringBuffer中,在内存里指向堆中的value,value指向最终的结果
  2. String变量的intern为常量池中结果的地址

内存布局:
在这里插入图片描述

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值