Java实验四 : Java API

实验目的:在集成开发环境下,通过阅读Java API文档来使用合适的常用类及方法解决实际问题。

实验目标:
(1) 能够通过阅读Java API文档来灵活运用Java API中的一些常用类(例如String、StringBuffer、System、Runtime、Math、Random等)来解决实际问题。
(2) 能够灵活运用用Java语言的常用用集合类(ArrayList、Map、Collections、Array等)来解决实际问题。

实验内容:
① 定义一个方法根据输入的身份证号判断这个人是否是陕西省西安市未央区的男性。
② 定义一个方法采用Random类、集合类(比如ArrayList)来获取三种彩票的数组(22选5,33选7,双色球等,注意只能用一个方法),返回值为字符串类型(如22选5:22,20,1,8,13)。
③ 获取当前的系统日期和时间来产生订单的单号,单号的构成如下:D+年份+月份+日期+时钟+分钟+秒钟+系统用户账号,要用到日期类Date(通过使用API文档来掌握该类的用法)。

1. 身份证号判断

public class IDnumber {

    //身份证号检查
    private static boolean IDnumberChack(String idNum) {
        boolean ret = true;
        for(int i = 0; i < idNum.length(); i++) {
            char ch = idNum.charAt(i);
            //检查前17位是否都是数字
            if(i < 17 && (ch < '0' || ch > '9')) {
                ret = false;
                break;
            }

            //检查第18位是否是数字或者x/X
            if(i == 17) {
                if((ch >= '0' && ch <= '9') || ch == 'X' || ch == 'x')
                    ret = true;
                else
                    ret = false;
            }
        }
        return ret;
    }


    //处理身份证号的静态方法
    public static void handleIDnumber(String idNum) throws IDnumberException{
        if(idNum.length() != 18) {
            throw new IDnumberException("身份证号不是18位!");
        }
        else {
            if(!IDnumberChack(idNum)) {
                throw new IDnumberException("身份证号中存无效字符!");
            }
            else {
                //身份证号没问题
                print(idNum);
            }
        }
    }

    //打印函数
    private static void print(String idNum) {
        if(getSex(idNum).equals("男")) {
            if(isWeiyang(idNum)) {
                System.out.println("这个人是陕西省西安市未央区的男性");
                return;
            }
        }
        //能走到这说明不满足条件
        System.out.println("这个人不是陕西省西安市未央区的男性");
    }

    //获取性别
    private static String getSex(String idNum) {
        String tmp = idNum.substring(16, 17);
        if((Integer.parseInt(tmp)) % 2 == 0) {
            //偶数
            return "女";
        }
        else {
            //奇数
            return "男";
        }
    }

    private static boolean isWeiyang(String idNum) {
        if(idNum.substring(0, 6).equals("610112"))
            return true;
        else
            return false;
    }

}



异常类和测试类

public class IDnumberException extends Exception{
    public IDnumberException(String message) {
        super(message);
    }
}


public class Test {
    public static void main(String[] args) {
        try {
            IDnumber.handleIDnumber("141028200010100030");
            IDnumber.handleIDnumber("610112200001010031");
        }
        catch(IDnumberException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

2. 获取随机数

import java.util.Arrays;
import java.util.Random;

//获取三种彩票的数组
public class Lottery {

    //1. 22选5
    public static String Choose5() {
        int[] arr = new int[23]; //标记数组
        Arrays.fill(arr, 1); //全初始化为1
        int count = 0; //计数器
        int rand = 0; //临时获取随机数
        Random random = new Random();
        int[] tmp = new int[5]; //保存结果

        //产生五个随机数
        while(true) {
            //获取[1, 22)之间的随机数
            rand = random.nextInt(22) + 1;
            //判断是否有重复
            if(arr[rand] == 1) {
                arr[rand]--;
                tmp[count++] = rand;
            }
            else
                continue;
            if(count == 5)
                break;
        }
        String ret = "22选5: ";
        for (int i = 0; i < count; i++) {
            ret += String.valueOf(tmp[i]) + " ";
        }
        return ret;

        /*
        System.out.print("22选5: ");
        for(int i = 0; i < count; i++) {
         System.out.print(String.valueOf(tmp[i]) + " ");
        }
        */
    }

    //33选7
    public static String Choose7() {
        int[] arr = new int[34]; //标记数组
        Arrays.fill(arr, 1); //全初始化为1
        int count = 0; //计数器
        int rand = 0; //临时获取随机数
        Random random = new Random();
        int[] tmp = new int[7]; //保存结果

        //产生五个随机数
        while(true) {
            //获取[1, 22)之间的随机数
            rand = random.nextInt(33) + 1;
            //判断是否有重复
            if(arr[rand] == 1) {
                arr[rand]--;
                tmp[count++] = rand;
            }
            else
                continue;
            if(count == 7)
                break;
        }

        String ret = "33选7: ";
        for (int i = 0; i < count; i++) {
            ret += String.valueOf(tmp[i]) + " ";
        }
        return ret;

        /*
        System.out.print("33选7: ");
        for(int i = 0; i < count; i++) {
            System.out.print(String.valueOf(tmp[i]) + " ");
        }
        */
    }

    //双色球
    //33个红球中选6个, 16个篮球中选1个
    public static String rbBall() {
        int[] arr = new int[34]; //标记数组
        Arrays.fill(arr, 1); //全初始化为1
        int count = 0; //计数器
        int rand = 0; //临时获取随机数
        Random random = new Random();
        int[] tmp = new int[6]; //保存结果

        //产生五个随机数
        while(true) {
            //获取[1, 22)之间的随机数
            rand = random.nextInt(33) + 1;
            //判断是否有重复
            if(arr[rand] == 1) {
                arr[rand]--;
                tmp[count++] = rand;
            }
            else
                continue;
            if(count == 6)
                break;
        }

        String ret = "红色球: ";
        for (int i = 0; i < count; i++) {
            ret += String.valueOf(tmp[i]) + " ";
        }

        ret += "  蓝色球: ";
        rand = random.nextInt(16) + 1;
        ret += String.valueOf(rand) + " ";

        return ret;

        /*
        System.out.print("红色球: ");
        for(int i = 0; i < count; i++) {
            System.out.print(String.valueOf(tmp[i]) + " ");
        }

        System.out.println();
        System.out.print("蓝色球: ");
        rand = random.nextInt(16) + 1;
        System.out.println(rand);
         */
    }

}

测试类

public class Test {
    public static void main(String[] args) {
        System.out.println(Lottery.Choose5());

        System.out.println(Lottery.Choose7());

        System.out.println(Lottery.rbBall());
    }
}

3. 生成订单号

import java.util.Calendar;
import java.util.Date;

public class OrderNumber {
    //生成新的订单号
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int min = calendar.get(Calendar.MINUTE);
        int sec = calendar.get(Calendar.SECOND);

        System.out.println("订单号为: D" + year + month + day + hour + min + sec + System.getProperty("user.name"));
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殇&璃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值