String&Array字符串|数组练习

String&Array

  • 练习
  • 关于String类的方法和Array的操作
  • 字符串分割,数组排序,字符串赋值给自定义对象并添加到数组中
题目:
  1. 从键盘上输入身份证号, 判断出生日期,性别,
    身份证倒数第二位表示性别,双数为女性,单为男

  2. 有字符串: ”101,lisi,98;

    202,wangwu,76;

    303,chenqi,84;

    404,zhangsan,49;

    505,xiaoming,67”,

    保存的学生的学号,姓名,成绩信息.

    要求把字符串中学生信息取出来,创建Student对象,

    把Student对象保存到数组中;遍历学生对象数组;

    在数组中查找名字为xiaoxiao同学是否存在.对数组中学生对象根据成绩降序排序


import java.util.Scanner;

/**
 * @ClassName Judge
 * @Deacription TODO 1. 从键盘上输入身份证号, 判断出生日期,性别
 *                   2. 身份证倒数第二位表示性别,双数为女性,单为男
 * @Version 1.0
 */
public class Judge {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean verify = true;
        while (verify){
            System.out.print("请输入你的身份证号码:");
            String id = sc.next();
            // 用正则表达验证控制台输入
            boolean control = id.matches("\\d{17}(\\d|X|x)");
            if (control){
                // 截取身份证中出生日期
                String birth = id.substring(6, 14);
                // 取出倒数第二位的元素,偶数为女性
                char c = id.charAt(id.length() - 2);
                if (c % 2 == 0){
                    System.out.println("女性\t" + "出生日期:" + birth);
                    // 判断成功结束
                    verify = false;
                }else {
                    System.out.println("男性\t" + "出生日期:" + birth);
                    verify = false;
                }
            }else{
                //不合法继续循环
                System.out.println("身份证输入不合法...请重新输入!");
            }
        }
    }
}
Student类
public class Student implements Comparable<Student>{
    private String id;
    private String name;
    private String score;

    public Student(String id, String name, String score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }
    public Student() {
    }

    public String getId() {
        return id;
    }

    public Student setId(String id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public Student setName(String name) {
        this.name = name;
        return this;
    }

    public String getScore() {
        return score;
    }

    public Student setScore(String score) {
        this.score = score;
        return this;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", score='" + score + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return o.getScore().compareTo(this.getScore());
    }
}
测试类
/**
 * @ClassName StudentTest
 * @Deacription
 *  TODO
 *     有字符串: ”101,lisi,98;
 *              202,wangwu,76;
 *              303,chenqi,84;
 *              404,zhangsan,49;
 *              505,xiaoming,67”,
 *      保存的学生的学号,姓名,成绩信息.
 *      要求把字符串中学生信息取出来,创建Student对象,
 *      把Student对象保存到数组中;遍历学生对象数组;
 *      在数组中查找名字为xiaoxiao同学是否存在.对数组中学生对象根据成绩降序排序
 * @Version 1.0
 */
public class StudentTest {
    public static void main(String[] args) {
        String src = "101,lisi,98;202,wangwu,76;303,chenqi,84;404,zhangsan,49;505,xiaoming,67";
        // 分号替换成逗号
        String replace = src.replace(";", ",");
        // 通过逗号分隔字符串
        String[] split = replace.split(",");
        for (String s : split) {
            System.out.print(s + " ");
        }
        //创建学生数组
        Student[] stus = new Student[5];
        // 思路,内循环,分隔字符串后并按照每3个字符分别赋值给不同的对象
        // split[i]元素下标为持续+1,定义初始值,循环自增
        int index = 0;
        for (int i = 0; i < stus.length ; i++) {
                String id = split[index++];
                String name = split[index++];
                String score = split[index++];
                Student student = new Student(id,name,score);
                stus[i] = student;
        }
        System.out.println();
        // 遍历学生对象,未排序
        for (Student student : stus) {
            System.out.println(student);
        }
//        对自定义类型进行排序需要实现Comparable并实现CompareTo方法重写规则
//        否则报错Exception in thread "main" java.lang.ClassCastException
        Arrays.sort(stus); // sort默认升序,已在Student类重新编写排序规则
        //判断学生是否存在
        if (isExist1(stus,"xiaoxiao")){
            System.out.println("学生存在");
        }else{
            System.out.println("不存在");
        }
        // 按照成绩降序输出
        System.out.println("按照学生成绩降序输出");
        for (Student student : stus) {
            System.out.println(student);
        }
    }

    public static boolean isExist1(Student[] stus ,String target){
        for (int i = 0; i < stus.length; i++) {
            if (target.equals(stus[i].getName())){
                return true;
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值