一: enum示例的运行结果和结论。
- 枚举类型是引用类型
- 枚举不属于原始数据类型,每个枚举值都是常量
- 枚举值可以通过values()方法获取所有常量
- 枚举类型可以用于switch语句
- 枚举类型是类型安全的,编译时会检查类型
二: testdouble示例的运行结果和结论。
1.结果 0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999
2.结论 运算结果与预期精确值存在微小差异,这是因为浮点数在计算机中是以二进制表示的,有些十进制小数无法精确转换为二进制小数。
三: XY不同加的结果和结论。
1.结果
X+Y=100200
300=X+Y
2.结论
Java中+运算符的运算顺序是从左到右,但当遇到字符串时,后续的+会被当作字符串连接符而非算术加法。
四: 使用java回答课后作业第一题。
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class MathQuestionGenerator {
public static void main(String[] args) {
// 生成 30 道四则运算题
String[] questions = generateMathQuestions(30);
// 打印题目for (int i = 0; i < questions.length; i++) {System.out.println("第" + (i + 1) + "题:" + questions[i]);}// 保存题目到文件saveQuestionsToFile(questions, "math_questions.txt");
}/*** 生成指定数量的四则运算题** @param count 题目数量* @return 题目数组*/
public static String[] generateMathQuestions(int count) {String[] questions = new String[count];Random random = new Random();for (int i = 0; i < count; i++) {int num1 = random.nextInt(11); // 生成 0-10 的随机数int num2 = random.nextInt(11); // 生成 0-10 的随机数char operator = getRandomOperator(random); // 随机选择运算符// 处理减法,确保结果非负if (operator == '-') {if (num1 < num2) {int temp = num1;num1 = num2;num2 = temp;}}questions[i] = num1 + " " + operator + " " + num2 + " = ";}return questions;
}/*** 获取随机运算符(+ 或 -)** @param random 随机数生成器* @return 运算符*/
public static char getRandomOperator(Random random) {return random.nextBoolean() ? '+' : '-';
}/*** 将题目保存到文件** @param questions 题目数组* @param fileName 文件名*/
public static void saveQuestionsToFile(String[] questions, String fileName) {try (FileWriter writer = new FileWriter(fileName)) {for (int i = 0; i < questions.length; i++) {writer.write("第" + (i + 1) + "题:" + questions[i] + "\n");}System.out.println("题目已保存到文件:" + fileName);} catch (IOException e) {System.err.println("保存文件时发生错误:" + e.getMessage());}
}
}