常用类作业

 


1.字符串反转

(1)将字符串中指定部分进行反转。比如将"abcdef"反转为"aedcbf";

(2)编写方法 public static String reverse(String str, int start , int end)搞定。

package com.yt.homework;

public class Homework1 {
    public static void main(String[] args) {
        String str = "abcdef";
        System.out.println("原来的字符串s1=" + str);
        String reverse = null;
        try {
            reverse = reverse(str, 1, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        System.out.println("反转之后的字符串s2=" + reverse);


    }
    public static String reverse(String str, int start, int end){

        //对输入的参数做一个验证
        //重要的编程思想!!!
        //(1)写成正确的情况
        //(2)然后取反即可
        if (!(str != null && start>=0 && start<end && end<str.length())){
            throw new RuntimeException("参数不正确");
        }

        char[] chars = str.toCharArray();
        char temp = ' ';
        for (int i = start; i < end; i++) {
            while (start < end){
                temp = chars[start];
                chars[start] = chars[end];
                chars[end] = temp;
                start++;
                end--;
            }
        }
        //重新构建一个String来返回即可
        return new String(chars);
    }
}

2.注册题处理

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

(1)用户名长度为2或3或4。

(2)密码的长度为6,要求全是数字isDigital。

(3)邮箱中包含@和.;并且@在.的前面。

package com.yt.homework;

import java.util.Scanner;

public class Homework2 {
    public static void main(String[] args) {
        String name = "abc";
        String password = "123456";
        String email = "111@qq.com";

        try {
            userRegister(name,password,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("参数不能为null");
        }

        //判断用户名是否正确
        int userLength = name.length();
        if (!(userLength>=2 && userLength<=4)){
            throw new RuntimeException("用户名的长度为2或3或4");
        }

        //判断密码输入是否正确
        int pwdLength = pwd.length();
        if (!(pwdLength==6 && isDigtal(pwd))){
            throw new RuntimeException("密码长度为6,且全部是数字");
        }

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

    }

    //判断是不是全部是数字
    public static boolean isDigtal(String pwd){
        char[] chars = pwd.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

public class Homework03 {
    public static void main(String[] args) {
        String name = "Willian Jefferson Clinton";
        printName(name);
    }

    /**
     * 编写方法: 完成输出格式要求的字符串
     * 编写java程序,输入形式为: Han shun Ping的人名,以Ping,Han .S的形式打印
     *       出来    。其中.S是中间单词的首字母
     * 思路分析
     * (1) 对输入的字符串进行 分割split(" ")
     * (2) 对得到的String[] 进行格式化String.format()
     * (3) 对输入的字符串进行校验即可
     */
    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);
    }
}
输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字
public class Homework04 {
    public static void main(String[] args) {
            String str = "abcHsp U 1234";
            countStr(str);
    }

    /**
     * 输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字
     * 思路分析
     * (1) 遍历字符串,如果 char 在 '0'~'9' 就是一个数字
     * (2) 如果 char 在 'a'~'z' 就是一个小写字母
     * (3) 如果 char 在 'A'~'Z' 就是一个大写字母
     * (4) 使用三个变量来记录 统计结果
     */
    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);
    }
}

4.内存布局测试

1671373599145

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;
    }
}
posted @   半路_出家ren  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
返回顶端
点击右上角即可分享
微信分享提示