今日报告
今天完成了软件构造的作业,用类的封装完成加减乘除混合运算的题目列表
MathQuestion.Java
import java.util.Random; // 题目类 public class MathQuestion { private String question; private String answer; public MathQuestion(int operand1, int operand2, int operator) { String operatorSymbol; int result; switch (operator) { case 0: operatorSymbol = "+"; result = operand1 + operand2; break; case 1: operatorSymbol = "-"; result = operand1 - operand2; break; case 2: operatorSymbol = "*"; result = operand1 * operand2; break; case 3: operatorSymbol = "/"; result = operand1 / operand2; break; default: throw new IllegalArgumentException("Invalid operator: " + operator); } question = operand1 + " " + operatorSymbol + " " + operand2 + " = "; answer = Integer.toString(result); } public String getQuestion() { return question; } public String getAnswer() { return answer; } }
ExamPaper.Java
import java.util.Random; // 试卷类 public class ExamPaper { private MathQuestion[] questions; public ExamPaper(int numberOfQuestions) { questions = generateQuestions(numberOfQuestions); } private MathQuestion[] generateQuestions(int numberOfQuestions) { MathQuestion[] questions = new MathQuestion[numberOfQuestions]; Random random = new Random(); for (int i = 0; i < numberOfQuestions; i++) { int operand1 = random.nextInt(100); // 第一个操作数,范围:0-99 int operand2 = random.nextInt(100); // 第二个操作数,范围:0-99 int operator = random.nextInt(4); // 运算符,0表示加法,1表示减法,2表示乘法,3表示除法 questions[i] = new MathQuestion(operand1, operand2, operator); } return questions; } public void printPaper() { System.out.println("试卷:"); for (int i = 0; i < questions.length; i++) { System.out.println("题目 " + (i + 1) + ": " + questions[i].getQuestion()); } } public void printAnswers() { System.out.println("答案:"); for (int i = 0; i < questions.length; i++) { System.out.println("题目 " + (i + 1) + ": " + questions[i].getQuestion() + " " + questions[i].getAnswer()); } } }
Main.Java
// 主函数类 public class Main { public static void main(String[] args) { int numberOfQuestions = 10; ExamPaper paper = new ExamPaper(numberOfQuestions); paper.printPaper(); paper.printAnswers(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南