尚学堂java 参考答案 第八章

一.选择题

1.BD

解析:B:Integer是对象,所以默认的应该是null对象.D使用的是自动装箱

2.A

解析:String类的对象是final型,是不能修改的,concat()方法是生成一个新的字符串,不是修改原字符串

3.B

解析:https://blog.csdn.net/qq_34834846/article/details/81431103

4.C

5.A

解析:https://blog.csdn.net/qq_34834846/article/details/81626022

二.简答题

1.http://www.sxt.cn/Java_jQuery_in_action/eight-autoboxing-unboxing.html

2.http://www.sxt.cn/Java_jQuery_in_action/eight-buffebuild.html

3.第一个是常量对象,第二个是String对象

4.mkdir创建目录时必须保证所有上级目录存在,如果缺失一个则失败,mkdirs若缺少则自动创建相关目录

5.http://www.sxt.cn/Java_jQuery_in_action/eight-enumeration.html

三.编码题

import java.util.Scanner;

public class ch8_1 {
    public static void main(String args[]) {
        String name="";
        String reg ="[0-9]";//正则表达式,表示0-9的数字
        Scanner input = new Scanner(System.in);

        System.out.print("请输入您的姓名:");
        name = input.nextLine();
        //这里的替换函数是replaceAll(),不是replace
        int length = name.replaceAll(reg,"").length();



        while(length <= 6 || name.length() != length){
            if(length <= 6)
                System.out.println("姓名不能小于7位 !");
            if(name.length() != length)
                System.out.println("姓名含有数字 !");



            System.out.print("请输入您的姓名:");
            name = input.nextLine();
            length = name.replaceAll(reg,"").length();
        }

        System.out.println("你的名字:"+name);

    }


}

2.题意不清,没有写

3.

import java.util.Random;


public class ch8_2 {
    public static void main(String[] args) {

        Random random = new Random();


        while(true){
            int a = random.nextInt(3);
            switch (TrafficLight.values()[a]){
                case RED:
                    System.out.println("红灯:停止");
                case YELLOW:
                    System.out.println("黄灯:慢行");
                case GREEN:
                    System.out.println("绿灯:行驶");

            }

        }



    }
}
enum TrafficLight{
    RED,YELLOW,GREEN
        }
原文地址:https://www.cnblogs.com/jeasion/p/10758369.html