使用JavaSwing要求:显示5道算式,并允许用户填入结果,单击检查按钮后,显示分数。请直接给出代码

时间: 2023-08-31 13:21:57 浏览: 68
### 回答1: 在这里给出一个示例代码,这是使用 Java Swing 实现的简单算式测试程序,可以供您参考: ``` import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class Quiz extends JFrame implements ActionListener { // 定义 5 个算式和答案 private int[][] equations = new int[][] { {3, 4, 7}, {5, 5, 10}, {7, 8, 15}, {9, 10, 19}, {11, 12, 23} }; // 定义 5 个文本框,用于输入结果 private JTextField[] answers = new JTextField[5]; public Quiz() { // 设置窗口标题 setTitle("算式测试"); // 设置布局 setLayout(new GridLayout(6, 2)); // 循环显示算式 for (int i = 0; i < 5; i++) { // 显示算式 add(new JLabel(equations[i][0] + " + " + equations[i][1] + " = ")); // 创建文本框 answers[i] = new JTextField(10); // 添加文本框 add(answers[i]); } // 创建检查按钮 JButton checkButton = new JButton("检查"); // 添加按钮监听器 checkButton.addActionListener(this); // 添加按钮 add(checkButton); // 设置窗口大小 setSize(400, 200); // 设置窗口可见 setVisible(true); } public static void main(String[] args) { new Quiz(); } @Override public void actionPerformed(ActionEvent e) { // 统计分数 int score = 0; // 循环检查答案 for (int i = 0; i < 5; i++) { // 获取文本框中的答案 int answer = Integer.parseInt ### 回答2: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MathQuiz extends JFrame { private JLabel[] equations; private JTextField[] answerFields; private JButton checkButton; public MathQuiz() { super("Math Quiz"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(6, 2)); equations = new JLabel[5]; answerFields = new JTextField[5]; for (int i = 0; i < 5; i++) { equations[i] = new JLabel(generateEquation()); answerFields[i] = new JTextField(); add(equations[i]); add(answerFields[i]); } checkButton = new JButton("Check"); checkButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { checkAnswers(); } }); add(new JLabel()); add(checkButton); pack(); setVisible(true); } private String generateEquation() { int operand1 = (int) (Math.random() * 10) + 1; int operand2 = (int) (Math.random() * 10) + 1; char operator; switch ((int) (Math.random() * 4)) { case 0: operator = '+'; break; case 1: operator = '-'; break; case 2: operator = '*'; break; case 3: operator = '/'; break; default: operator = '+'; } return operand1 + " " + operator + " " + operand2 + " = "; } private void checkAnswers() { int correctCount = 0; for (int i = 0; i < 5; i++) { String answer = answerFields[i].getText(); String[] parts = equations[i].getText().split(" "); int operand1 = Integer.parseInt(parts[0]); int operand2 = Integer.parseInt(parts[2]); int expectedAnswer = 0; switch (parts[1].charAt(0)) { case '+': expectedAnswer = operand1 + operand2; break; case '-': expectedAnswer = operand1 - operand2; break; case '*': expectedAnswer = operand1 * operand2; break; case '/': expectedAnswer = operand1 / operand2; break; } if (answer.equals(String.valueOf(expectedAnswer))) { correctCount++; } } double score = (double) correctCount / 5; JOptionPane.showMessageDialog(this, "Your score: " + score); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MathQuiz(); } }); } } 使用Java的Swing编写一个简单的数学测验程序。该程序将显示5道随机生成的算式,并允许用户填入结果。用户单击检查按钮后,程序会检查用户的答案,并显示测验分数。每道算式都是由两个1到10之间的随机整数和一个随机运算符(加号、减号、乘号或除号)组成。程序使用JFrame作为GUI窗口,并使用GridLayout来排列组件。 该程序的核心是MathQuiz类,它继承自JFrame并实现了ActionListener接口。MathQuiz类包括了5个JLabel和5个JTextField来显示算式和接收用户的答案。还包括一个JButton来触发检查答案的操作。checkAnswers()方法用于检查用户的答案,并计算测验得分。 在main()方法中,我们使用SwingUtilities的invokeLater()方法来创建并显示MathQuiz对象,以保证在事件分发线程上运行GUI代码。 这个代码提供了一个简单的Math Quiz游戏框架,您可以根据自己的需要进行扩展和优化。 ### 回答3: 以下是使用Java Swing实现的代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class EquationGame extends JFrame implements ActionListener { private JLabel equationLabel1, equationLabel2, equationLabel3, equationLabel4, equationLabel5; private JTextField resultField1, resultField2, resultField3, resultField4, resultField5; private JButton checkButton; private JLabel scoreLabel; public EquationGame() { setTitle("Equation Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLayout(new GridLayout(7, 2)); equationLabel1 = new JLabel("2 + 3 = "); resultField1 = new JTextField(); add(equationLabel1); add(resultField1); equationLabel2 = new JLabel("4 * 5 = "); resultField2 = new JTextField(); add(equationLabel2); add(resultField2); equationLabel3 = new JLabel("10 / 2 = "); resultField3 = new JTextField(); add(equationLabel3); add(resultField3); equationLabel4 = new JLabel("7 - 3 = "); resultField4 = new JTextField(); add(equationLabel4); add(resultField4); equationLabel5 = new JLabel("8 % 4 = "); resultField5 = new JTextField(); add(equationLabel5); add(resultField5); checkButton = new JButton("检查"); checkButton.addActionListener(this); add(checkButton); scoreLabel = new JLabel(""); add(scoreLabel); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == checkButton) { int score = 0; if (resultField1.getText().equals("5")) { score++; } if (resultField2.getText().equals("20")) { score++; } if (resultField3.getText().equals("5")) { score++; } if (resultField4.getText().equals("4")) { score++; } if (resultField5.getText().equals("0")) { score++; } scoreLabel.setText("得分: " + score + "/5"); } } public static void main(String[] args) { new EquationGame(); } } ``` 这个程序创建了一个带有5个算式和答案输入框的窗口。用户可以填写各个算式的答案,并单击检查按钮后,会根据用户的答案显示得分。

相关推荐

最新推荐

recommend-type

Java编程实现swing圆形按钮实例代码

主要介绍了Java编程实现swing圆形按钮实例代码,涉及两个简单的Java实现按钮的代码,其中一个具有侦测点击事件的简单功能,具有一定借鉴价值,需要的朋友可以参考。
recommend-type

Java Swing JButton按钮的实现示例

主要介绍了Java Swing JButton按钮的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

情人节写给女朋友Java Swing代码程序

主要为大家分享了情人节写给女朋友的java小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,祝大家每天都是情人节
recommend-type

Java实现时间动态显示方法汇总

主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下
recommend-type

Java Swing JLabel标签的使用方法

主要介绍了Java Swing JLabel标签的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

前端深拷贝 和浅拷贝有哪些方式,你在哪里使用过

前端深拷贝和浅拷贝的方式有很多,下面列举几种常用的方式: 深拷贝: 1. JSON.parse(JSON.stringify(obj)),该方法可以将对象序列化为字符串,再将字符串反序列化为新的对象,从而实现深拷贝。但是该方法有一些限制,例如无法拷贝函数、RegExp等类型的数据。 2. 递归拷贝,即遍历对象的每个属性并进行拷贝,如果属性值是对象,则递归进行拷贝。 3. 使用第三方库如lodash、jQuery等提供的深拷贝方法。 浅拷贝: 1. Object.assign(target, obj1, obj2, ...),该方法可以将源对象的属性浅拷贝到目标对象中,如果有相同的属性,则会
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。