用户输入学号,如果是以aabcddef开头,并且后边是4位数字,前两位大于06小于等于当前年份。判断用户输入是否合法

 

package seven_topic;

import java.util.*;

public class p_19_1 {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入学生学号: ");
        String a = reader.next();
        String LF4 = a.substring(a.length() - 4);// 后四
        String lf2 = LF4.substring(0, 2);// 后四前二

        boolean b = true;
        try {
            @SuppressWarnings("unused")
            int n1 = Integer.parseInt(LF4);// 后四位转整
            int n2 = Integer.parseInt(lf2);
            if (n2 >= 06 && n2 <= 22) {
                b = true;
            } else {
                b = false;
            }
        } catch (Exception e) {
            b = false;
        }
        if (a.startsWith("aabcddef") && b == true) {
            System.out.println("学生学号录入成功!");
        } else {
            System.out.println("学生学号格式错误!");
        }
    }
}

 

posted @ 2022-05-18 21:29  飞鱼Dai  阅读(50)  评论(0)    收藏  举报