day16作业

一.填空题

1.Integer Character 2.String s = "123";Integer i = new Integer(s);System.out.println(i.intValue());

3.StringBuffer  4.StringBuffer  5.public final String format(Date date)   6.System.out.println(Math.round(Math.random()*30+30));   7.enum

二.选择题

1.BD  2.D  3.D  4.D  5.C  6.AB  7.D  8.A  9.A  10.C

三.判断题

1.×  2.×  3.×  4.√  5.√

四.简答题

1.

自动装箱:将基本数据类型转换为包装类类型

自动拆箱:将包装类类型转换为基本数据类型

2.String类的API概述是这样的:String类代表字符串,Java程序中的所有字符串字面值都作为此类的实例体现。字符串是常量,它们的值在创建之后不能更改。可见,String是对象且为不可变对象,一旦被创建,就不能被改变,对于已经存在的String类的对象的更改都是在常量池中重新创建一个对象,将这个新地址覆盖原来的地址值,原来的就变成垃圾了。

StringBuffer类API概述:线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。可见StringBuffer是一个可变对象,当对它进行修改时不会像String那样重新创建对象,它只能通过构造函数来建立对象,当对象建立后,在内存中分配空间,空参构造StringBuffer()构造一个不带字符的字符串缓冲区,其初始容量为16个字符。可以通过append方法向StringBuffer中赋值。

在线程安全上,StringBuffer是线程安全的,StringBuilder是线程不安全的。举个例子StringBuffer好比火车上的厕所,进去之后只能容纳一个人,门从里面反锁,外面的人进不去只能z在外面排队等候,故StringBuffer是线程安全的,同时StringBuffer效率也低。反之,虽然StringBuilder线程不安全但是效率高。

package com.zuikc.homework;

import java.util.Scanner;

public class Test6 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String admin = sc.nextLine();

        for (int i = 0; i < admin.length(); i++) {
            char c = admin.charAt(i);
            if ((c >= '0' && c <= '9') && (admin.length() > 6) && (admin.isEmpty() == true)) {
                System.out.println("用户名有误");
                break;
            } 
        }

    }

}
package com.zuikc.homework;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test7 {

    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年龄:");
        String age = sc.nextLine();
        System.out.println("请输入分数:");
        String goal = sc.nextLine();
        System.out.println("请输入入学时间:");
        String date = sc.nextLine();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date d = sdf.parse(date);
        System.out.println(Integer.parseInt(age));
        System.out.println(Double.parseDouble(goal));
        System.out.println(d.getTime());
    }

}
package com.zuikc.homework;

import java.util.Random;

public class Test8 {

    public static void main(String[] args) {
        demo1();
        demo2();
        demo3();
    }

    private static void demo3() {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.round(r.nextDouble()*10 + 13));
        }
    }

    private static void demo2() {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(r.nextInt(10) + 13);
        }
    }

    private static void demo1() {
        for (int i = 0; i < 10; i++) {
            System.out.println((int)(Math.random()*10 + 13));
        }
    }

}
原文地址:https://www.cnblogs.com/lyx210019/p/9357072.html