Java - 常用类 - 结业 - 课堂练习

编写方法,实现指定字符串翻转

翻转全部

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 14:03
 */
public class Homework01 {
    public static void main(String[] args) {
        String a = "1234567";
        System.out.println(reverse(a));
    }
    public static String reverse(String str){
        char[] str2 = str.toCharArray();

        String str3 = "";
        for (int i = str2.length - 1; i >= 0; i--) {
            str3 += str2[i];
        }
        return str3;

    }
}

翻转指定索引

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 14:03
 */
public class Homework01 {
    public static void main(String[] args) {
        String a = "1234567";

        try {
            System.out.println(reverse(a,1,3));
        } 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[] str2 = str.toCharArray();

        String str3 = "";

        str3 += str.substring(0,start);

        for (int i = end; i >= start; i--) {
            str3 += str2[i];
        }
        str3 += str.substring(end + 1);
        return str3;

    }
}

输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象

  1. 用户名长度为2或3或4
  2. 密码的长度为6,要求全是数字
  3. 邮箱中包含@和.并且@在.的前面

Mycode

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 15:09
 */
public class Homework02 {
    public static void main(String[] args) {
        try {
            register("涛涛","124567","2965883296@qq.com");
        } catch (Exception e) {
            System.out.println("请检查你的输入格式!!");
        }
    }
    public static void register(String name_,String password_,String email_){
        int count = 0;
        if(name_.length() > 1 && name_.length() <= 4){
            System.out.println("姓名格式正确");
            count++;
        }else{
            System.out.println("姓名输入错误");
        }


        if(password_.length() == 6){
            try {
                int num = Integer.parseInt(password_);      //转int
                System.out.println("密码格式正确");
                count++;
            } catch (NumberFormatException e) {
                System.out.println("只能输入数字");
            }
        }else{
            System.out.println("密码输入错误");
        }
        //如果邮箱中能找到@和.,并且@的索引比.要小(非)
        if(!(email_.indexOf("@") != -1 && email_.indexOf(".") != -1 && email_.indexOf("@") < email_.indexOf("."))){
            System.out.println("邮箱输入错误");
        }else{
            System.out.println("邮箱格式正确");
            count++;
        }

        if(count == 3){
            System.out.println("注册成功");
        }else{
            System.out.println("please check our ");
        }

    }
}

Teacher’s code

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 15:09
 */
public class Homework02 {
    public static void main(String[] args) {
        try {
            register("涛涛","124567","2965883296@qq.com");
        } catch (Exception e) {
            System.out.println("请检查你的输入格式!!");
        }
    }
    public static void register(String name_,String password_,String email_){
        //防止参数为空
        if(!(name_ != null && password_ != null && email_ != null)){
            throw new RuntimeException("参数不能为空");
        }

        if(!(name_.length() > 1 && name_.length() <= 4)){
            throw new RuntimeException("用户名长度为2或3或4");
        }


        if(!(password_.length() == 6 && isDigital(password_))){    //调用方法  true true
            throw new RuntimeException("密码只能6位纯数字");
        }

        //如果邮箱中能找到@和.,并且@的索引比.要小(非)
        if(!(email_.indexOf("@") != -1 && email_.indexOf(".") != -1 && email_.indexOf("@") < email_.indexOf("."))){
            throw new RuntimeException("邮箱中必须存在@和.,并且@在.前面");
        }

        System.out.println("注册成功");

    }

    //密码数字判断
    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;
    }
}

编写java程序

输入形式为:“Willian Jefferson Clinton”,输出形式为:Clinton,Willian.J

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 15:59
 */
public class Homework03 {
    public static void main(String[] args) {
        String name = null;
        try {
            name = "Willian Jefferson Clinton";
            formatName(name);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
    public static void formatName(String name){
        if (name == null){
            throw new RuntimeException("输入姓名不能为空");
        }
        String[] arr = name.split(" ");
        if(arr.length != 3){
            throw new RuntimeException("请检查输入格式");
        }
        arr[2] += ",";
        arr[0] += ".";
        arr[1] = arr[1].substring(0,1);
        System.out.print(arr[2] + arr[0] + arr[1]);
    }
}

输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字

Mycode
package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 16:55
 */
public class Homework04 {
    public static void main(String[] args) {
        String a = "fadsfasdfFFFSFSFS45646464";
        findChar(a);
    }
    public static void findChar(String str){    //方法名小驼峰
        int number = 0;
        int lowerCase = 0;
        int upperCase = 0;
        char[] chars = str.toCharArray();
        for(char i:chars){
            if(i >= 48 && i <= 57){
                number++;
            }else if(i >= 65 && i <= 90){
                upperCase++;
            }else{
                lowerCase++;
            }
        }
        System.out.println("大写字母有:" + upperCase + "\n小写字母有:" +
                lowerCase + "\n数字有:" + number);
    }
}

Teacher’s code
package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 16:55
 */
public class Homework04 {
    public static void main(String[] args) {
        String a = "fadsfasdfFFFSFSFS45646464";
        findChar(a);
    }
    public static void findChar(String str){    //方法名小驼峰
        int number = 0;
        int lowerCase = 0;
        int upperCase = 0;
        char[] chars = str.toCharArray();
        for(char i:chars){
            if(i >= '0' && i <= '9'){
                number++;
            }else if(i >= 'A' && i <= 'Z'){
                upperCase++;
            }else{
                lowerCase++;
            }
        }
        System.out.println("大写字母有:" + upperCase + "\n小写字母有:" +
                lowerCase + "\n数字有:" + number);
    }
}

看代码,画出String内存关系图

package com.tao.homework.string_;

/**
 * Create By 刘鸿涛
 * 2022/1/4 17:25
 */
public class Homework05 {
    public static void main(String[] args) {
        String s1 = "taotao";
        Animal a = new Animal(s1);
        Animal b = new Animal(s1);
        System.out.println(a == b);             //F     两个对象
        System.out.println(a.equals(b));        //F     Animal类没有重写equals ,判断是否同一个对象
        System.out.println(a.name == b.name);   //T     指向同一个空间

        String s4 = new String("taotao");   //新对象 value数组
        String s5 = "taotao";                   //指向存在的常量池

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

        String t1 = "hello" + s1;               //堆中开 value数组
        String t2 = "hellotaotao";              //存在后指向常量池
        System.out.println(t1.intern() == t2);     //t1.intern()指向池 T
    }
}
class Animal{
    String name;

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

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值