练习 String翻转 注册处理 字符串统计

p493

将字符串中指定部分进行翻转

package chapter;

public class reverse {
    public static void main(String[] args) {
        String str = "abcdef";
        str = reverseMethod(str,0,3);
        System.out.println(str);
    }

    public static String reverseMethod(String str, int start, int end) {
        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);
    }
}

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

package chapter;

public class HomeWork {
    public static void main(String[] args) {
        String name = "asd";
        String pwd = "548745";
        String email= "asd@sd.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.length()>=2 && name.length()<=4)){
            throw new RuntimeException("用户名长度错误");
        }

        // 密码
        if(!(pwd.length()==6 && isDigital(pwd) )){
            throw new RuntimeException("密码错误");
        }

        //邮箱
        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 < str.length(); i++) {
            if(chars[i] < '0' || chars[i] >'9'){
                return false;
            }
        }return true;
    }
}

输入 Tony Smith Willam 输出 Willam  Tony W

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值