Day14练习

A:简答题

1、 把我们今天讲解过的所有类中的方法在API中找到,并自己加上描述

String:
    public boolean matches(String regex) 
	判断字符串是否与正则表达式相匹配
    public String[] split(String regex)
	分割满足regex正则表达式的字符串
    public String replaceAll(String regex,String replacement) 
	用字符串replacement替换满足正则表达式的字符串
Math:
    public static int abs(int a)
	取绝对值
    public static double ceil(double a) 
	向上取整
    public static double floor(double a) 
	向下取整
    public static int max(int a,int b) 
	获取最大值
    public static double pow(double a,double b) 
	获取a的b次方
    public static double random()
	获取0~1之间的随机数
    public static int round(float a) 
	四舍五入
    public static double sqrt(double a)	
	获取a的正平方根
Random:
    public int nextInt()
	生成int范围内的随机数
    public int nextInt(int n) 
	生成0~n的随机数
System
    public static void gc()
	垃圾回收器
    public static void exit(int status) 
	退出java虚拟机 0正常退出 非0异常退出
    public static long currentTimeMillis()
	获取当前时间距离1970-1-1 00:00:00的毫秒数
BigDecimal
    public BigDecimal add(BigDecimal augend) 
	加
    public BigDecimal subtract(BigDecimal subtrahend) 
	减
    public BigDecimal multiply(BigDecimal multiplicand) 
	乘
    public BigDecimal divide(BigDecimal divisor) 
	除(能整除的情况)
    public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
    除(不能整除的情况) 保留小数点后scale为 roundingMode取舍模式
Date
    public long getTime()
	返回一个日期对象对应的毫秒值
    public void setTime(long time)
	给一个日期对象设置指定毫秒值
SimpleDateFormat
    public final String format(Date date) 
	将一个日期对象格式化一个字符串
    public Date parse(String source) 
	讲一个日期字符串转换成一个日期对象
Calendar
    public static Calendar getInstance()
	获得一个日历对象
    public int get(int field)	
	获得给定日历对应的值
    public void add(int field,int amount) 
	
    public final void set(int year,int month,int date) 
    

2、 什么是正则表达式?

正则表达式就是正确规则的表达式
用来描述或匹配符合某个句法规则的字符串

3、 如何实现Date与long相互转换?

	Date date = new Date();
Date -> long
	date.getTime();
long -> Date
	date.setTime();

4、 如何实现Date与String相互转换?

	Date date = new Date();
	SimpleDateFormat s = new SimpleDateFormat();
Date -> String
	String format = s.format(date);
String -> Date
	Date parse = s.parse(format);

B:编程题

1、请编写程序,校验键盘录入的电子邮箱是否合法,并测试

public static void main(String[] args) {
    //校验键盘录入的电子邮箱是否合法

    //键盘录入邮箱
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入网易邮箱");
    String str = sc.nextLine();
    //6~18 个字符,可使用字母、数字、下划线,需要以字母开头
    //编写正则表达式
    String regex = "[a-zA-Z]\\w{5,17}@163.com";
    //判断字符串是否满足正则表达式规则
    boolean matches = str.matches(regex);
    if (matches == true){
        System.out.println("输入格式正确");
    }else {
        System.out.println("输入格式有误");
    }
}

2、请编写程序,把给定字符串中的数字排序

给定的字符串是: “91 27 -45 46 38 50”

最终输出结果是: “-45 27 38 46 50 91”

public static void main(String[] args) {
    //给定的字符串是: "91 27 -45 46 38 50"
    //最终输出结果是: "-45 27 38 46 50 91"
    String s = "91 27 -45 46 38 50";
    //通过正则表达式切割字符串 返回一个String类型数组
    String[] arr = s.split(" ");
    //将Sting类型的数组转换成int类型数组
    int[] newArr = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        newArr[i] = Integer.parseInt(arr[i]);
    }
    //通过StringBuilder将newArr数组拼接成字符串
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < newArr.length; i++) {
        sb.append(newArr[i]).append(" ");
    }
    //将StringBuilder转换成String
    String str = new String(sb).trim();
    System.out.println(str);
    //91 27 -45 46 38 50
}

3、请编写程序,完成获取指定的日期与今天相距多少天,并测试

public static void main(String[] args) throws ParseException {
    Scanner sc = new Scanner(System.in);
    System.out.println("输入一个日期 格式: 1970-01-01");
    String inDate = sc.next();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date parse = sdf.parse(inDate);//输入的日期
    long inTime = parse.getTime();//输入日期的毫秒值
    long time = System.currentTimeMillis();//当前日期的毫秒值
    long day = (time - inTime) / 1000 / 60 / 60 / 24;
    System.out.println("指定的日期与今天相距" + day + "天");
}

4、请编写程序,完成文件路径的切割,并测试

public static void main(String[] args) {
    //完成文件路径的切割
    String str = "D:\\CloudMusic\\CloudMusic_workspace\\Cache";
    String[] split = str.split("\\\\");
    System.out.println(Arrays.toString(split));
    //[D:, CloudMusic, CloudMusic_workspace, Cache]
}

5、请编写程序,校验键盘录入的用户名是否合法,并测试

要求:用户名必须是6-16位之间的字母下划线或者数字

public static void main(String[] args) {
    //校验键盘录入的用户名是否合法
    //用户名必须是6-16位之间的字母下划线或者数字

    //键盘录入
    Scanner sc = new Scanner(System.in);
    System.out.println("输入用户名");
    String name = sc.nextLine();
    //通过正则表达式判断name是否合法
    boolean matches = name.matches("\\w{6,16}");
    if (matches == true){
        System.out.println("用户名合法");
    }else {
        System.out.println("用户名不合法");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值