使用JavaSwing要求:显示5道算式,并允许用户填入结果,单击检查按钮后,显示分数。请直接给出代码
时间: 2023-08-31 20:21:57 浏览: 124
Java编程实现swing圆形按钮实例代码
5星 · 资源好评率100%
### 回答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个算式和答案输入框的窗口。用户可以填写各个算式的答案,并单击检查按钮后,会根据用户的答案显示得分。
阅读全文