chapter12homework

package chapter12homework;
//        (1)将字符串中指定部分进行反专。比如将"abcdef"反转为"aedcbf"
//        (2)编写方法public static String reverse(String str, int start , int end)搞定
public class homwWork01 {
    public static void main(String[] args) {
        String str = "abscd";
        String str1 = "aaa";
        try{//进行异常处理
            str1 = reverse(str,2,4);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(str1);
    }
    public static String reverse(String str, int start , int end){
        char[] chars = str.toCharArray();
        char temp;
        //最好加上异常处理,判断字符串是否为空,start和end的大小是不是正确,提高程序的健壮性
        if(!(str != null && start < end && start >= 0 && end < str.length())){
            throw new RuntimeException("输入错误");
        }
        while(end - start >= 1){//也可以写成end > start
            temp = chars[start];
            chars[start] = chars[end];
            chars[end] = temp;
            start++;
            end--;
        }
        return new String(chars);
    }
}
package chapter12homework;

import java.util.Scanner;

//输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象要求:
//        (1)用户名长度为2或3或4
//        (2)密码的长度为6,要求全是数字isDigital
//        (3)邮箱中包含@和。并且@在.的前面
//和老师的不一样,自己直接写成了一个类,但是老师写的一个方法,这个没写完
public class HomeWork02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        Person p1 = new Person(str,"123","1234");
        System.out.println(p1.getName());
    }
}
class Person{
    private String name;

    public Person(String name, String passWord, String email) {
        if(name.length() < 2 || name.length() > 4){
            System.out.println("名字长度有误");
        }
        else{
            this.name = name;
        }
        if(passWord.length() == 6){

        }
            this.passWord = passWord;
            this.email = email;


    }

    private String passWord;
    private String email;

    public String getName() {
        return name;
    }

    public String getPassWord() {
        return passWord;
    }

    public String getEmail() {
        return email;
    }
}
package chapter12homework;
//输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象要求:
//        (1)用户名长度为2或3或4
//        (2)密码的长度为6,要求全是数字isDigital
//        (3)邮箱中包含@和。并且@在.的前面
//问题:对于第二个需求,另外写了一个方法来判断是不是数字
//      对于第三个需求,获得@和.的位置可以直接使用indexof方法

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

    }
    public static void userRegister(String name,String passWord,String email){
        if(!(name.length() >= 2 && name.length() <= 4)){
            throw new RuntimeException("名字长度输入错误");
        }
        char[] chars = passWord.toCharArray();
        for(int i = 0;i < chars.length;i++){
            if(!(Character.isDigit(chars[i]))){
                throw new RuntimeException("输入的密码不是数字");
            }
        }
        char[] chars1 = email.toCharArray();
        int j = 0;
        for(int i = 0;i < chars1.length;i++){
            if(chars1[i] == '@'){
                j = i;
                break;
            }
        }
        int k = 0;
        if(j == 0){
            throw new RuntimeException("输入邮箱格式错误");
        }else {
            for(;j < chars1.length; j++){
                if(chars1[j] == '.'){
                    k = j;
                    break;
                }
            }
        }
        if(k == 0){
            throw new RuntimeException("输入邮箱格式错误");
        }

    }
}
package chapter12homework;
//        (1)编写java程序,输入形式为:Han Shun Ping的人名,以Ping,Han .S的形式打印
//        出来。其中.S是中间单词的首字母。
//        (2)例如输入“Willian Jefferson Clinton”,输出形式为:Clinton, Willian .J
//        可以用fomat进行格式化
//        可以对对输入的字母进行校验
public class HomeWork03 {
    public static void main(String[] args) {
        String str = "Willian Jefferson Clinton";
        String[] s = str.split(" ");
        System.out.println(s[2] + ", " + s[0] + " ." + s[1].substring(0,1));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值