Java语言程序设计(基础篇)(原书第10版) 练习题答案 第4章

时间:2021-10-07 23:28:51

基础篇课后习题答案,做了大部分,虽然不一定是最佳代码,但是保证每个都能运行,如有更好的答案,欢迎讨论


4.1 计算五边形面积

Scanner scanner = new Scanner(System.in);
System.out.print("请输入五边形从中心点到顶点的距离:");
double r = scanner.nextDouble();
double s = 2 * r * (Math.sin(Math.PI / 5));
double area = (5 * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / 5));
System.out.printf("五边形的面积为:%1.2f", area);


4.4 计算六边形面积

Scanner scanner = new Scanner(System.in);
System.out.print("请输入六边形的边长:");
double s = scanner.nextDouble();
double area = (6 * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / 6));
System.out.printf("六边形的面积为:%1.2f", area);


4.8 ASCII对应字符

Scanner scanner = new Scanner(System.in);
System.out.print("请输入ASCII码(0~127):");
int ascii = scanner.nextInt();
char c = (char) ascii;
System.out.print("对应的字符为:" + c);


4.9 字符对应ASCII

Scanner scanner = new Scanner(System.in);
System.out.print("请输入字符:");
String str = scanner.nextLine();
char c = str.charAt(0);
int i = (int) c;
System.out.print("对应的ASCII码为:" + i);


4.11 十进制转十六进制

Scanner scanner = new Scanner(System.in);
System.out.print("请输入十进制数(0~15):");
int num = scanner.nextInt();
if (num >= 0 && num <= 9) {
System.out.print("对应十六进制数为:" + num);
} else if (num >= 10 && num <= 15) {
int i = num - 10 + 'A';
System.out.print("对应十六进制数为:" + (char) i);
} else {
System.out.print("输入范围有误!");
}


4.12 十六进制转二进制

Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个十六进制数:");
String str = scanner.nextLine();
int i = Integer.parseInt(str, 16);
String out = Integer.toBinaryString(i);
System.out.print("对应的二进制为:" + out);


4.13 判断元音还是辅音

Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字母:");
String str = scanner.nextLine();

if (str.length() != 1) {
System.out.print("输入字母超长!");
System.exit(1);
}

if (str.compareToIgnoreCase("A") >= 0 && str.compareToIgnoreCase("Z") <= 0) {
if (str.equalsIgnoreCase("A")) {
System.out.print(str + " 为元音");
} else if (str.equalsIgnoreCase("E")) {
System.out.print(str + " 为元音");
} else if (str.equalsIgnoreCase("U")) {
System.out.print(str + " 为元音");
} else if (str.equalsIgnoreCase("I")) {
System.out.print(str + " 为元音");
} else if (str.equalsIgnoreCase("O")) {
System.out.print(str + " 为元音");
} else {
System.out.print(str + " 为辅音");
}
} else {
System.out.print("输入无效!");
}


4.14 字母转换为数字

Scanner scanner = new Scanner(System.in);
System.out.print("请输入任一字母 (A,B,C,D,F) :");
String str = scanner.nextLine();

switch (str) {
case "A":
System.out.print("对应的数字为 4");
break;
case "B":
System.out.print("对应的数字为 3");
break;
case "C":
System.out.print("对应的数字为 2");
break;
case "D":
System.out.print("对应的数字为 1");
break;
case "F":
System.out.print("对应的数字为 0");
break;
default:
System.out.print("请输入正确数字!");
break;
}


4.15 电话键盘

Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字母:");
String str = scanner.nextLine();

switch (str.toUpperCase()) {
case "A":
case "B":
case "C":
System.out.print("对应的数字是 2");
break;
case "D":
case "E":
case "F":
System.out.print("对应的数字是 3");
break;
case "G":
case "H":
case "I":
System.out.print("对应的数字是 4");
break;
case "J":
case "K":
case "L":
System.out.print("对应的数字是 5");
break;
case "M":
case "N":
case "O":
System.out.print("对应的数字是 6");
break;
case "P":
case "Q":
case "R":
case "S":
System.out.print("对应的数字是 7");
break;
case "T":
case "U":
case "V":
System.out.print("对应的数字是 8");
break;
case "W":
case "X":
case "Y":
case "Z":
System.out.print("对应的数字是 9");
break;
default:
System.out.print("请输入一个有效的字母!");
break;
}


4.16 随机字符

int i = (int) Math.round(Math.random() * 25);
int j = (int) 'A' + i;
char ch = (char) j;
System.out.print("产生的随机数是:" + ch);


4.17 计算月份天数

Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
Scanner input = new Scanner(System.in);
System.out.print("请输入月份前三个字母:");
String month = input.nextLine();

switch (month.toUpperCase()) {
case "JAN":
System.out.print("该月份有31天");
break;
case "FEB":
if (year % 4 == 0) {
System.out.print("该月份有29天");
break;
} else {
System.out.print("该月份有28天");
break;
}
case "MAR":
System.out.print("该月份有31天");
break;
case "APR":
System.out.print("该月份有30天");
break;
case "MAY":
System.out.print("该月份有31天");
break;
case "JUN":
System.out.print("该月份有30天");
break;
case "JUL":
System.out.print("该月份有31天");
break;
case "AUG":
System.out.print("该月份有31天");
break;
case "SEP":
System.out.print("该月份有30天");
break;
case "OCT":
System.out.print("该月份有31天");
break;
case "NOV":
System.out.print("该月份有30天");
break;
case "DEC":
System.out.print("该月份有31天");
break;
}


4.18 年级和专业

Scanner scanner = new Scanner(System.in);
System.out.print("请输入专业字符(M代表数学、C代表计算机、I代表信息):");
String str = scanner.nextLine();
System.out.print("请输入年级:");
int num = scanner.nextInt();

switch (str.toUpperCase()) {
case "M":
switch (num) {
case 1:
System.out.print("数学系大一学生");
break;
case 2:
System.out.print("数学系大二学生");
break;
case 3:
System.out.print("数学系大三学生");
break;
case 4:
System.out.print("数学系大四学生");
break;
default:
System.out.print("无效输入");
System.exit(1);
break;
}
break;
case "C":
switch (num) {
case 1:
System.out.print("计算机系大一学生");
break;
case 2:
System.out.print("计算机系大二学生");
break;
case 3:
System.out.print("计算机系大三学生");
break;
case 4:
System.out.print("计算机系大四学生");
break;
default:
System.out.print("无效输入");
System.exit(1);
break;
}
break;
case "I":
switch (num) {
case 1:
System.out.print("信息系大一学生");
break;
case 2:
System.out.print("信息系大二学生");
break;
case 3:
System.out.print("信息系大三学生");
break;
case 4:
System.out.print("信息系大四学生");
break;
default:
System.out.print("无效输入");
System.exit(1);
break;
}
break;
default:
System.out.print("无效输入");
System.exit(1);
break;
}


4.20 显示长度和首字母

Scanner scanner = new Scanner(System.in);
System.out.print("请输入一句话:");
String str = scanner.nextLine();
System.out.print("该句话共有" + str.length() + "个字符,其中首字母是" + str.charAt(0));


4.21 检查SSN

Scanner scanner = new Scanner(System.in);
System.out.print("请输入SSN号:");
String str = scanner.nextLine();

if (str.length() != 11) {
System.out.print("输入格式错误!");
System.exit(1);
}

char ch0 = str.charAt(0);
char ch1 = str.charAt(1);
char ch2 = str.charAt(2);
char ch3 = str.charAt(3);
char ch4 = str.charAt(4);
char ch5 = str.charAt(5);
char ch6 = str.charAt(6);
char ch7 = str.charAt(7);
char ch8 = str.charAt(8);
char ch9 = str.charAt(9);
char ch10 = str.charAt(10);

if (Character.isDigit(ch0) &&
Character.isDigit(ch1) &&
Character.isDigit(ch2) &&
ch3 == '-' &&
Character.isDigit(ch4) &&
Character.isDigit(ch5) &&
ch6 == '-' &&
Character.isDigit(ch7) &&
Character.isDigit(ch8) &&
Character.isDigit(ch9) &&
Character.isDigit(ch10)) {
System.out.print(str + "是一个正确的SSN格式");
} else {
System.out.print("输入格式错误!");
System.exit(1);
}


4.22 判断子句子

Scanner scanner1 = new Scanner(System.in);
System.out.print("请输入第一个句子:");
String str1 = scanner1.nextLine();

Scanner scanner2 = new Scanner(System.in);
System.out.print("请输入第二个句子:");
String str2 = scanner2.nextLine();

if (str1.contains(str2)) {
System.out.print("第一个句子包含第二个句子");
} else {
System.out.print("第一个句子不包含第二个句子");
}


4.24 城市排序

Scanner scanner1 = new Scanner(System.in);
System.out.print("请输入第一个城市:");
String str1 = scanner1.nextLine();

Scanner scanner2 = new Scanner(System.in);
System.out.print("请输入第二个城市:");
String str2 = scanner2.nextLine();

Scanner scanner3 = new Scanner(System.in);
System.out.print("请输入第三个城市:");
String str3 = scanner3.nextLine();

char ch1, ch2, ch3;
String str_tmp;

int length1 = str1.length();
int length2 = str2.length();
int length3 = str3.length();
int max = (length1 > length2) ? length1 : length2;
max = (max > length3) ? max : length3;

for (int i = 0; i < max; i++) {
ch1 = str1.toLowerCase().charAt(i);
ch2 = str2.toLowerCase().charAt(i);
if (ch1 > ch2) {
str_tmp = str1;
str1 = str2;
str2 = str_tmp;
break;
}
}

for (int i = 0; i < max; i++) {
ch2 = str2.toLowerCase().charAt(i);
ch3 = str3.toLowerCase().charAt(i);
if (ch2 > ch3) {
str_tmp = str2;
str2 = str3;
str3 = str_tmp;
break;
}
}

for (int i = 0; i < max; i++) {
ch1 = str1.toLowerCase().charAt(i);
ch2 = str2.toLowerCase().charAt(i);
if (ch1 > ch2) {
str_tmp = str1;
str1 = str2;
str2 = str_tmp;
break;
}
}

System.out.print("排序后的结果是:" + str1 + "," + str2 + "," + str3);


4.25 生成车牌号

String CPT_str = "";
String NUM_str = "";
for (int i = 1; i <= 3; i++) {
int num = (int) (Math.random() * 26);
num = (int) 'A' + num;
char CPT = (char) num;
CPT_str = CPT_str + String.valueOf(CPT).toString();
}
for (int i = 1; i <= 4; i++) {
int num = (int) (Math.random() * 10);
NUM_str = NUM_str + String.valueOf(num).toString();
}

System.out.print("随机生成的车牌号为:" + CPT_str + NUM_str);


Java语言程序设计(基础篇)(原书第10版) 练习题答案 第4章