Java 15.8.1 实验1 算术测试

本文介绍了一款用于训练小学生算术能力的小型软件。该软件包含三个主要类:Teacher、ComputerFrame 和 MainClass。Teacher 类负责生成算术题目并验证答案;ComputerFrame 类提供了用户界面,包括输入框和按钮等;MainClass 则是启动类。

实验要求

   编写一个算术测试的小软件,用来训练小学生的算术能力。程序由3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确,ComputerFrame对象负责为算术题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案,MainClass是软件主类。



代码

MainClass.java

public class MainClass {

	public static void main(String[] args) {
		ComputerFrame frame;
		frame = new ComputerFrame();
		frame.setTitle("算数测试");
		frame.setBounds(100, 100, 650, 180);
	}

}
ComputerFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComputerFrame extends JFrame {
	JMenuBar menubar;
	JMenu choiceGrade;
	JMenuItem grade1, grade2;
	JTextField textOne, textTwo, textResult;
	JButton getProblem, giveAnswer;
	JLabel operatorLabel, message;
	Teacher teacherzhang;

	ComputerFrame() {
		teacherzhang = new Teacher();
		teacherzhang.setMaxInteger(20);
		setLayout(new FlowLayout());
		menubar = new JMenuBar();
		choiceGrade = new JMenu("选择级别");
		grade1 = new JMenuItem("幼儿级别");
		grade2 = new JMenuItem("儿童级别");
		grade1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				teacherzhang.setMaxInteger(10);
			}
		});
		grade2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				teacherzhang.setMaxInteger(50);
			}
		});
		choiceGrade.add(grade1);
		choiceGrade.add(grade2);
		menubar.add(choiceGrade);
		setJMenuBar(menubar);
		textOne = new JTextField(5);
		textTwo = new JTextField(5);
		textResult = new JTextField(5);
		operatorLabel = new JLabel("+");
		operatorLabel.setFont(new Font("Arial", Font.BOLD, 20));
		message = new JLabel("你还没有回答");
		getProblem = new JButton("获取题目");
		giveAnswer = new JButton("确认答案");
		add(textOne);
		add(operatorLabel);
		add(textTwo);
		add(new JLabel("="));
		add(textResult);
		add(giveAnswer);
		add(message);
		add(getProblem);
		textResult.requestFocus();
		textOne.setEditable(false);
		textTwo.setEditable(false);
		getProblem.setActionCommand("getProblem");
		textResult.setActionCommand("answer");
		giveAnswer.setActionCommand("answer");
		teacherzhang.setJTextField(textOne, textTwo, textResult);
		teacherzhang.setJLabel(operatorLabel, message);
		getProblem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				teacherzhang.actionPerformed(e);
			}
		});
		giveAnswer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				teacherzhang.actionPerformed(e);
			}
		});
		textResult.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				teacherzhang.actionPerformed(e);
			}
		});
		setVisible(true);
		validate();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

}

Teacher.java

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;

public class Teacher {
	int numberOne, numberTwo;
	String operator = "";
	boolean isRight;
	Random random;
	int maxInteger;
	JTextField textOne, textTwo, textResult;
	JLabel operatorLabel, message;

	Teacher() {
		random = new Random();
	}

	public void setMaxInteger(int n) {
		maxInteger = n;
	}

	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("getProblem")) {
			numberOne = random.nextInt(maxInteger) + 1;
			numberTwo = random.nextInt(maxInteger) + 1;
			double d = Math.random();
			if (d >= 0.5)
				operator = "+";
			else
				operator = "-";
			textOne.setText("" + numberOne);
			textTwo.setText("" + numberTwo);
			operatorLabel.setText(operator);
			message.setText("请回答");
			textResult.setText(null);
		} else if (str.equals("answer")) {
			String answer = textResult.getText();
			try {
				int result = Integer.parseInt(answer);
				if (operator.equals("+")) {
					if (result == numberOne + numberTwo)
						message.setText("你的回答正确");
					else
						message.setText("你的回答错误");
				} else if (operator.equals("-")) {
					if (result == numberOne - numberTwo)
						message.setText("你的回答正确");
					else
						message.setText("你的回答错误");
				}
			} catch (NumberFormatException ex) {
				message.setText("请输入数字字符");
			}
		}
	}

	public void setJTextField(JTextField... t) {
		textOne = t[0];
		textTwo = t[1];
		textResult = t[2];
	}

	public void setJLabel(JLabel... label) {
		operatorLabel = label[0];
		message = label[1];
	}
}

运算结果



{ "name": "edx", "version": "0.1.0", "repository": "https://github.com/openedx/edx-platform", "scripts": { "postinstall": "scripts/copy-node-modules.sh", "build": "npm run webpack && npm run compile-sass", "build-dev": "cross-env npm run webpack-dev && npm run compile-sass-dev", "webpack": "cross-env NODE_ENV=development webpack --config webpack.dev.config.js", "webpack-dev": "NODE_ENV=development webpack --config=webpack.dev.config.js", "compile-sass": "scripts/compile_sass.py --env=${NODE_ENV:-production}", "compile-sass-dev": "scripts/compile_sass.py --env=development", "watch": "{ npm run watch-webpack& npm run watch-sass& } && sleep infinity", "watch-webpack": "npm run webpack-dev -- --watch", "watch-sass": "scripts/watch_sass.sh", "test": "npm run test-jest && npm run test-karma", "test-jest": "jest", "test-karma": "npm run test-karma-vanilla && npm run test-karma-require && echo 'WARNING: Skipped broken webpack tests. For details, see: https://github.com/openedx/edx-platform/issues/35956'", "test-karma-vanilla": "npm run test-cms-vanilla && npm run test-xmodule-vanilla && npm run test-common-vanilla", "test-karma-require": "npm run test-cms-require && npm run test-common-require", "test-karma-webpack": "npm run test-cms-webpack && npm run test-lms-webpack && npm run test-xmodule-webpack", "test-karma-conf": "${NODE_WRAPPER:-xvfb-run --auto-servernum} node --max_old_space_size=4096 node_modules/.bin/karma start --single-run=true --capture-timeout=60000 --browsers=FirefoxNoUpdates", "test-cms": "npm run test-cms-vanilla && npm run test-cms-require && npm run test-cms-webpack", "test-cms-vanilla": "npm run test-karma-conf -- cms/static/karma_cms.conf.js", "test-cms-require": "npm run test-karma-conf -- cms/static/karma_cms_squire.conf.js", "test-cms-webpack": "npm run test-karma-conf -- cms/static/karma_cms_webpack.conf.js", "test-lms": "npm run test-jest && npm run test-lms-webpack", "test-lms-webpack": "npm run test-karma-conf -- lms/static/karma_lms.conf.js", "test-xmodule": "npm run test-xmodule-vanilla && npm run test-xmodule-webpack", "test-xmodule-vanilla": "npm run test-karma-conf -- xmodule/js/karma_xmodule.conf.js", "test-xmodule-webpack": "npm run test-karma-conf -- xmodule/js/karma_xmodule_webpack.conf.js", "test-common": "npm run test-common-vanilla && npm run test-common-require", "test-common-vanilla": "npm run test-karma-conf -- common/static/karma_common.conf.js", "test-common-require": "npm run test-karma-conf -- common/static/karma_common_requirejs.conf.js" }, "dependencies": { "@babel/core": "7.26.0", "@babel/plugin-proposal-object-rest-spread": "^7.18.9", "@babel/plugin-transform-object-assign": "^7.18.6", "@babel/preset-env": "^7.19.0", "@edx/brand": "npm:@openedx/brand-openedx@^1.2.2", "@edx/edx-bootstrap": "1.0.4", "@edx/edx-proctoring": "^4.18.1", "@edx/frontend-component-cookie-policy-banner": "2.2.0", "@edx/paragon": "2.6.4", "@edx/studio-frontend": "^2.1.0", "@testing-library/jest-dom": "^6.6.3", "@testing-library/react": "^12.1.5", "@testing-library/user-event": "^12.8.3", "babel-loader": "^9.1.3", "babel-plugin-transform-class-properties": "6.24.1", "babel-polyfill": "6.26.0", "backbone": "1.6.0", "backbone-associations": "0.6.2", "backbone.paginator": "2.0.8", "bootstrap": "4.0.0", "camelize": "1.0.1", "classnames": "2.5.1", "cross-env": "^10.0.0", "css-loader": "7.1.2", "datatables": "1.10.18", "datatables.net-fixedcolumns": "5.0.4", "edx-ui-toolkit": "1.8.7", "exports-loader": "0.6.4", "file-loader": "^6.2.0", "font-awesome": "4.7.0", "hls.js": "0.14.17", "imports-loader": "0.8.0", "jest-environment-jsdom": "^29.0.0", "jquery": "2.2.4", "jquery-migrate": "1.4.1", "jquery.scrollto": "2.1.3", "js-cookie": "3.0.5", "moment": "2.30.1", "moment-timezone": "0.5.47", "node-gyp": "11.1.0", "popper.js": "1.16.1", "prop-types": "15.8.1", "raw-loader": "0.5.1", "react": "16.14.0", "react-dom": "16.14.0", "react-focus-lock": "^1.19.1", "react-redux": "5.1.2", "react-router-dom": "5.3.4", "react-slick": "0.30.3", "redux": "3.7.2", "redux-thunk": "2.2.0", "requirejs": "2.3.7", "rtlcss": "4.3.0", "sass": "^1.54.8", "sass-loader": "^16.0.0", "scriptjs": "2.5.9", "style-loader": "4.0.0", "svg-inline-loader": "0.8.2", "uglify-js": "3.19.3", "underscore": "1.13.7", "underscore.string": "3.3.6", "webpack": "^5.90.3", "webpack-bundle-tracker": "3.2.0", "webpack-merge": "6.0.1", "which-country": "1.0.0" }, "devDependencies": { "@babel/preset-react": "^7.27.1", "@edx/mockprock": "1.0.2", "@edx/stylelint-config-edx": "2.3.3", "babel-jest": "29.7.0", "jasmine-core": "2.6.4", "jasmine-jquery": "2.1.1", "jest": "29.7.0", "karma": "^6.4.4", "karma-chrome-launcher": "^3.2.0", "karma-coverage": "2.2.1", "karma-firefox-launcher": "2.1.3", "karma-jasmine": "^5.1.0", "karma-jasmine-html-reporter": "0.2.2", "karma-junit-reporter": "2.0.1", "karma-requirejs": "1.1.0", "karma-selenium-webdriver-launcher": "latest", "karma-sourcemap-loader": "0.4.0", "karma-spec-reporter": "0.0.20", "karma-webpack": "^5.0.1", "plato": "1.7.0", "react-test-renderer": "16.14.0", "selenium-webdriver": "^2.44.0", "sinon": "19.0.2", "squirejs": "0.1.0", "string-replace-loader": "^3.1.0", "stylelint-formatter-pretty": "4.0.1", "underscore-template-loader": "^1.2.0", "webpack-cli": "^5.1.4" } } 如何运行起来
最新发布
09-25
<think>好的,用户现在有一个Open edX的项目,他们想运行起来。首先,我需要理解他们的需求。用户可能是一个开发者,或者是刚开始接触Open edX,想要在本地搭建环境进行开发或测试。他们提供的package.json文件显示这是一个前端项目,使用了npm脚本,包括构建、测试等任务。 首先,用户遇到的问题可能是如何正确安装依赖并运行项目。根据之前的对话历史,用户遇到了npm的EBADENGINE警告,这可能是因为依赖的karma版本过旧,不兼容当前的Node.js版本。用户可能已经尝试过安装依赖,但遇到了问题,或者不知道如何继续。 接下来,我需要考虑用户的环境。用户可能使用的是Windows系统,因为之前有将Bash脚本转换为PowerShell的请求。但当前的问题可能更偏向于通用的npm运行流程,不过需要考虑到不同操作系统的差异。 用户的需求是运行这个项目,所以需要指导他们完成从安装到运行的步骤。可能的步骤包括安装依赖、处理依赖冲突、运行构建和测试等。同时,用户可能对某些步骤不熟悉,比如环境变量配置、Node.js版本管理等,需要详细说明。 另外,用户可能没有说明是否已经安装了Node.js和npm,或者是否已经配置了正确的版本。因此,在回答中需要包含检查Node.js版本的步骤,并建议使用nvm来管理多个版本,特别是因为项目中的karma需要较低的Node版本。 还需要注意package.json中的scripts部分,比如postinstall脚本会运行copy-node-modules.sh,这可能在安装依赖后自动执行。用户可能需要确保这些脚本在Windows环境下也能正确运行,或者需要调整路径。 测试部分,用户可能会遇到karma测试的问题,尤其是因为karma版本较旧,可能需要额外的配置或替换测试框架。但用户可能更希望先成功运行项目,而不是立即解决测试问题,所以可以建议在成功运行后再处理测试相关的警告。 总结下来,回答需要分步骤指导用户安装依赖、处理Node版本、运行构建命令,并解释关键步骤的原因。同时,提供常见问题的解决方案,比如路径问题、权限问题等。还要确保用户了解如何在不同环境中调整脚本,特别是Windows用户需要注意的事项。 </think>以下是基于 Open edX 项目的完整启动指南,包含环境准备、依赖安装、构建流程和测试执行的系统性说明: --- ### **1. 环境准备** #### **1.1 Node.js 版本管理** ```bash # 推荐使用 nvm 管理 Node.js 版本(避免 EBADENGINE 警告) # 安装 nvm(Linux/macOS): curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.bashrc # 安装兼容版本(Karma 0.13.22 需要 Node.js 14) nvm install 14 nvm use 14 ``` #### **1.2 安装依赖** ```bash # 安装项目依赖(忽略引擎检查) npm install --engine-strict=false # 安装全局依赖(如 xvfb-run) npm install -g xvfb-run ``` --- ### **2. 项目构建** #### **2.1 开发环境构建** ```bash # 构建开发环境资源(包含 Webpack 和 Sass) npm run build-dev # 或单独运行 Webpack/Sass 构建 npm run webpack-dev npm run compile-sass-dev ``` #### **2.2 生产环境构建** ```bash # 构建生产环境资源 npm run build ``` --- ### **3. 开发模式监听** ```bash # 自动监听文件变化并重新构建 npm run watch # 单独监听 Webpack/Sass 变化 npm run watch-webpack npm run watch-sass ``` --- ### **4. 测试执行** #### **4.1 全量测试** ```bash # 运行所有测试(Jest + Karma) npm test ``` #### **4.2 分模块测试** ```bash # 单独运行 CMS 测试 npm run test-cms # 单独运行 LMS 测试 npm run test-lms # 单独运行 XModule 测试 npm run test-xmodule # 单独运行 Common 测试 npm run test-common ``` --- ### **5. 关键注意事项** #### **5.1 Karma 警告处理** - **警告原因**:`karma@0.13.22` 仅支持 Node.js 0.10/0.12/4/5,当前使用 Node.js 14。 - **临时方案**:使用 `nvm` 切换回 Node.js 14。 - **长期方案**:升级 Karma 及相关依赖(需修改 `package.json` 并验证兼容性)。 #### **5.2 Windows 环境适配** - **PowerShell 脚本**:将 `scripts/copy-node-modules.sh` 替换为 PowerShell 版本(如前文提供)。 - **路径问题**:确保所有路径使用 `\` 分隔符(如 `common\static\common\js\vendor`)。 #### **5.3 权限问题** ```bash # 如果遇到权限错误,尝试以管理员身份运行: sudo npm install --engine-strict=false ``` --- ### **6. 验证运行结果** 1. **检查输出目录**: - `common/static/css/`:应包含编译后的 CSS 文件。 - `common/static/js/`:应包含打包后的 JS 文件。 2. **运行测试覆盖率报告**: ```bash # 查看 Karma 生成的覆盖率报告(默认位于 coverage/ 目录) open coverage/index.html ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值