基础篇课后习题答案,做了大部分,虽然不一定是最佳代码,但是保证每个都能运行,如有更好的答案,欢迎讨论
3.1 解一元二次方程
double a, b, c, r1, r2; Scanner scanner = new Scanner(System.in); System.out.print("请输入a:"); a = scanner.nextDouble(); System.out.print("请输入b:"); b = scanner.nextDouble(); System.out.print("请输入c:"); c = scanner.nextDouble(); if (Math.pow(b, 2) - (4 * a * c) > 0) { r1 = (-b + Math.pow((Math.pow(b, 2) - (4 * a * c)), 0.5)) / 2 * a; r2 = (-b - Math.pow((Math.pow(b, 2) - (4 * a * c)), 0.5)) / 2 * a; System.out.print("方程式的两个根为:" + (double) Math.round(r1 * 1000) / 1000 + "和" + (double) Math.round(r2 * 1000) / 1000); } else if (Math.pow(b, 2) - (4 * a * c) == 0) { r1 = (-b + Math.pow((Math.pow(b, 2) - (4 * a * c)), 0.5)) / 2 * a; System.out.print("方程式只有一个根:" + (double) Math.round(r1 * 1000) / 1000); } else { System.out.print("该方程式无实数根"); }
3.3 解2乘2线性方程
double a, b, c, d, e, f, x, y; Scanner scanner = new Scanner(System.in); System.out.print("请输入a:"); a = scanner.nextDouble(); System.out.print("请输入b:"); b = scanner.nextDouble(); System.out.print("请输入c:"); c = scanner.nextDouble(); System.out.print("请输入d:"); d = scanner.nextDouble(); System.out.print("请输入e:"); e = scanner.nextDouble(); System.out.print("请输入f:"); f = scanner.nextDouble(); if ((a * d) - (b * c) == 0) { System.out.print("方程式无解"); } else { x = ((e * d) - (b * f)) / ((a * d) - (b * c)); y = ((a * f) - (e * c)) / ((a * d) - (b * c)); System.out.println("x 为:" + x); System.out.print("y 为:" + y); }
3.4 随机月份
int month = (int) (Math.random() * 12) + 1; System.out.println("随机数为:" + month); switch (month) { case 1: System.out.print("该月份为一月"); break; case 2: System.out.print("该月份为二月"); break; case 3: System.out.print("该月份为三月"); break; case 4: System.out.print("该月份为四月"); break; case 5: System.out.print("该月份为五月"); break; case 6: System.out.print("该月份为六月"); break; case 7: System.out.print("该月份为七月"); break; case 8: System.out.print("该月份为八月"); break; case 9: System.out.print("该月份为九月"); break; case 10: System.out.print("该月份为十月"); break; case 11: System.out.print("该月份为十一月"); break; case 12: System.out.print("该月份为十二月"); break; }
3.5 将来的日期
int day, after, newDay; Scanner scanner = new Scanner(System.in); System.out.print("今天是周几:"); day = scanner.nextInt(); System.out.print("请输入经过的天数:"); after = scanner.nextInt(); newDay = day + after; newDay = newDay % 7; switch (newDay) { case 0: System.out.print("周" + day + "经过" + after + "天是周日"); break; case 1: System.out.print("周" + day + "经过" + after + "天是周一"); break; case 2: System.out.print("周" + day + "经过" + after + "天是周二"); break; case 3: System.out.print("周" + day + "经过" + after + "天是周三"); break; case 4: System.out.print("周" + day + "经过" + after + "天是周四"); break; case 5: System.out.print("周" + day + "经过" + after + "天是周五"); break; case 6: System.out.print("周" + day + "经过" + after + "天是周六"); break; }
3.8 三个数排序
int a, b, c, tmp; Scanner input = new Scanner(System.in); System.out.print("请输入三个整数:"); a = input.nextInt(); b = input.nextInt(); c = input.nextInt(); if (a >= b) { tmp = a; a = b; b = tmp; } if (b >= c) { tmp = b; b = c; c = tmp; } if (a >= b) { tmp = a; a = b; b = tmp; } System.out.print("排列后的顺序为:" + a + "," + b + "," + c);
3.9 检查ISBN-10
int num, last; String str, isbn; Scanner input = new Scanner(System.in); System.out.print("请输入九位数字,第一位以0开头:"); num = input.nextInt(); str = "0" + String.valueOf(num).toString(); last = ((Integer.valueOf(str.substring(0, 1)).intValue() * 1) + (Integer.valueOf(str.substring(1, 2)).intValue() * 2) + (Integer.valueOf(str.substring(2, 3)).intValue() * 3) + (Integer.valueOf(str.substring(3, 4)).intValue() * 4) + (Integer.valueOf(str.substring(4, 5)).intValue() * 5) + (Integer.valueOf(str.substring(5, 6)).intValue() * 6) + (Integer.valueOf(str.substring(6, 7)).intValue() * 7) + (Integer.valueOf(str.substring(7, 8)).intValue() * 8) + (Integer.valueOf(str.substring(8, 9)).intValue() * 9)) % 11; if (last == 10) { isbn = str + "X"; } else { isbn = str + String.valueOf(last).toString(); } System.out.print("ISBN为:" + isbn);
3.12 回文数字
Scanner input = new Scanner(System.in); System.out.print("请输入三位数字:"); int num = input.nextInt(); if (num / 100 == num % 10) { System.out.print(num + "是回文数字"); } else { System.out.print(num + "不是回文数字"); }
3.15 彩票
int num = (int) (Math.random() * 1000); String strNum = String.format("%03d", num); System.out.println("随机数字已生成"); Scanner scanner = new Scanner(System.in); System.out.print("请输入三位数:"); int usrNum = scanner.nextInt(); String strUsrNum = String.format("%03d", usrNum); String num000 = strNum.substring(0, 1); String num00 = strNum.substring(1, 2); String num0 = strNum.substring(2, 3); String usrNum000 = strUsrNum.substring(0, 1); String usrNum00 = strUsrNum.substring(1, 2); String usrNum0 = strUsrNum.substring(2, 3); if (strNum.equals(strUsrNum)) { System.out.println("答案为:" + strNum); System.out.println("恭喜您,完全正确!奖金为10000美元"); } else if ((usrNum000.equals(num00) && usrNum00.equals(num0) && usrNum0.equals(num000)) || (usrNum000.equals(num000) && usrNum00.equals(num0) && usrNum0.equals(num00)) || (usrNum000.equals(num0) && usrNum00.equals(num00) && usrNum0.equals(num000)) || (usrNum000.equals(num0) && usrNum00.equals(num000) && usrNum0.equals(num00)) || (usrNum000.equals(num00) && usrNum00.equals(num000) && usrNum0.equals(num0))) { System.out.println("答案为:" + strNum); System.out.println("恭喜您,三个数字正确!奖金为3000美元"); } else if (usrNum000.equals(num000) || usrNum000.equals(num00) || usrNum000.equals(num0) || usrNum00.equals(num000) || usrNum00.equals(num00) || usrNum00.equals(num0) || usrNum0.equals(num000) || usrNum0.equals(num00) || usrNum0.equals(num0)) { System.out.println("答案为:" + strNum); System.out.println("恭喜您,包含正确数字!奖金为1000美元"); } else { System.out.println("答案为:" + strNum); System.out.println("很遗憾,没有回答正确"); }
3.17 猜拳
Scanner scanner = new Scanner(System.in); System.out.print("石头(0),剪刀(1),布(2),请出拳:"); int num = scanner.nextInt(); String[] name = {"石头", "剪刀", "布"}; System.out.println("你的出拳是......" + name[num]); int sysNum = (int) Math.round(Math.random() * 2); System.out.println("系统出拳是......" + name[sysNum]); switch (num) { case 0: switch (sysNum) { case 0: System.out.print("平局!"); break; case 1: System.out.print("你赢了!"); break; case 2: System.out.print("你输了!"); break; } break; case 1: switch (sysNum) { case 0: System.out.print("你输了!"); break; case 1: System.out.print("平局!"); break; case 2: System.out.print("你赢了!"); break; } break; case 2: switch (sysNum) { case 0: System.out.print("你赢了!"); break; case 1: System.out.print("你输了!"); break; case 2: System.out.print("平局!"); break; } break; }
3.24 抽纸牌
Scanner scanner = new Scanner(System.in); System.out.print("请用户抽取纸牌"); scanner.nextLine(); String[] num = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; String[] flower = {"黑桃", "红桃", "梅花", "方块"}; int numRandom = (int) Math.round(Math.random() * 13); int flowerRandom = (int) Math.round(Math.random() * 4); System.out.print("用户抽取的纸牌为:" + flower[flowerRandom] + num[numRandom]);