JAVA常用类作业(Chapter13)

第十三章作业

1.将字符串指定部分进行反转

  • 字符翻转 ,如何实现

  • 逻辑判断,选出正确的条件,然后取反即可。

  • 抛出异常,然后使用 try catch处理

    package com.homework13;
    
    /**
     * @author whj
     * @version 1.0
     */
    public class Homework01 {
        public static void main(String[] args) {
            String str = "abcdefghijklmn";
            try{
                str = reverse(str, 1, 5);
            } catch(Exception e){
                System.out.println(e.getMessage());
                return;
            }
            System.out.println(str);
        }
        public static String reverse(String str,int start,int end){
            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[j];
                chars[j] = chars[i];
                chars[i] = temp;
            }
    
            return new String(chars);
        }
    }
    

2.用户注册

  • 使用到的函数 str.indexof(’ ');//得到字符的位置

  • 对用户信息进行逻辑判断,选出正确的条件取反即可。

  • 字符串是否都是数字,将字符串转成数组,再判断每一项是否都在’0‘ - '9’之间即可;

  • 不满足条件,抛出异常即可

    package com.homework13;
    
    import java.util.Scanner;
    
    /**
     * @author whj
     * @version 1.0
     */
    public class Homework02 {
        public static void main(String[] args) {
            Person person = new Person();
            String name = "jack";
            String pwd = "123456";
            String email = "123@ .343";
    
            try {
                person.userRegister(name,pwd,email);
                System.out.println("注册成功");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.out.println("注册失败");
            }
            System.out.println(person.toString());
        }
    }
    class Person{
        private String name = " ";
        private String pwd = " ";
        private String email = " ";
    
        public Person() {
        }
    
        public Person(String name, String pwd, String email) {
            setName(name);
            setPwd(pwd);
            setEmail(email);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            if (name.length() > 4 || name.length() < 2){
    //            System.out.println("姓名长度输入错误(2-4之间)");
                throw new RuntimeException("姓名长度输入错误(2-4之间)");
            }
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            if (pwd.length()!=6){
    //            System.out.println("密码长度有误!!");
                throw new RuntimeException("密码长度有误!!");
    
            }
            char chars[] = pwd.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] > '9' || chars[i] < '0'){
    //                System.out.println("密码只能是数字");
                    throw new RuntimeException("密码只能是数字");
                }
            }
            this.pwd = pwd;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            int index1 = email.indexOf('@');
            int index2 = email.indexOf('.');
            if (!(index1 != -1  && index2 !=-1 && index1 == index2 -1)){
                System.out.println("格式错误");
                throw new RuntimeException("邮箱格式错误");
            }
            this.email = email;
        }
        public void userRegister(String name, String pwd, String email){
            setName(name);
            setPwd(pwd);
            setEmail(email);
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", pwd='" + pwd + '\'' +
                    ", email='" + email + '\'' +
                    '}';
        }
    }
    

3.按照格式输出名字信息

  • 函数:str.substring(start,end);//得到子字符串

  • split(" ");//按照空格分隔,老师用的这种,得到字符串数组

  • String.format()函数,按照一定格式转换得到字符串

package com.homework13;

/**
 * @author whj
 * @version 1.0
 */
public class Homework03 {
    public static void main(String[] args) {
        String name = "Hancvv ghunff Pfssing";
        printName(name);
    }
//    public static  void printName(String name){//简陋版本
//        int index1 = name.indexOf(' ');
//        int index2 = name.lastIndexOf(' ');
//        String str1 = name.substring(0,index1);
//        String str2 = name.substring(index1 + 1, index2  - 1);
//        String str3 = name.substring(index2 + 1);
//        System.out.println(str3 + "," + str1 + "." + str2.substring(0,1).toUpperCase());
//    }
    public static  void printName(String name){//简陋版本
        if (name == null){
            System.out.println("name 不能为空。");
            return;
        }
        String[] str = name.split(" ");
        if (str.length != 3){
            System.out.println("格式不正确。");
            return;
        }
        String format = String.format("%s,%s.%c", str[2], str[0], str[1].toUpperCase().charAt(0));
        System.out.println(format);

    }

}

4.判断字符类型

  • charAt函数

  • toCharArray()函数

    package com.homework13;
    
    /**
     * @author whj
     * @version 1.0
     */
    public class Homework04 {
        public static void main(String[] args) {
            count("ASAhha 11H");
        }
        public static void count(String str){
            if (str == null){
                System.out.println("不能为空。");
                return;
            }
            int counterNum = 0;
            int counterUp = 0;
            int counterLow = 0;
            int counterOther = 0
            char[] chars = str.toCharArray();//或是通过charAt()得到对应的字符
            for(int j = 0; j < chars.length; j++) {
                if (chars[j] >= '0' && chars[j] <= '9'){
                    counterNum++;
                }else if (chars[j] >= 'A' && chars[j] <= 'Z'){
                    counterUp++;
                }else if (chars[j] >= 'a' && chars[j] <= 'z'){
                    counterLow++;
                } else{
                    counterOther++;
                }
            }
            System.out.println("数字:" + counterNum +
                    "\n大写字母:" + counterUp +
                    "\n小写字母:" + counterLow);
        }
    }
    

5.String内存分布

  • 1.直接赋值,指向常量池;
  • 2.创建字符串对象,对象中的value指向常量池,若常量池中存在对应的字符串,直接指向该字符串;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:Educoder头歌实践平台 中 Java 数据结构之图 共4关 金币共2300 的那个实践项目答案由于图论问题渗透整个计算机科学,图算法对于计算机学科至关重要。成百上千的计算问题最后都可以归约为图论问题。引用:第一关public class HelloWorld{ /********* Begin *********/ public static void main(String[] args) { System.out.println("姓名:张三"); System.out.println("年龄:25"); System.out.println("职业:JAVA高级工程师"); System.out.println("薪资:15K"); } /********* End *********/ } 引用:第五关package chapter2; /********* Begin *********/ public class HelloWorld { String userName = "张三"; public static void main(String[] args){ System.out.println("hello eduCoder"); } } /********* End *********/。 根据提供的引用内容,我无法直接提供你所需的头歌实训作业答案。但是,我可以给你一些关于Java的一般知识,以帮助你完成头歌实训作业。 在Java中,你可以使用`public static void main(String[] args)`方法作为程序的起点。在这个方法中,你可以编写你想要执行的代码。例如,你可以使用`System.out.println`来输出一些文本。 如果你想定义一个变量,你可以使用`String`关键字,并指定变量的名称和初始值。例如,`String userName = "张三";`表示定义了一个名为`userName`的字符串变量,并将其初始化为"张三"。 希望这些信息对你有帮助。如果你有其他关于Java的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值