(大家好,今天分享的是Java的相关知识,大家可以在评论区进行互动答疑哦~加油!💕)
目录
Plug:程序实现
方法一(记事本)
- 新建一个记事本
- 输入代码
- 另存文件
- 更改文件名,名字为public class后的名字+.java(Hello.java)
- 更改保存类型,选择所有文件
- 选择编码格式,一般选择ANSI 或 UTF-8(在编译时需加上固定格式-encoding utf-8)(这里我们选择UTF-8)
方法二(IDEA)
- 新建java文件
- 输入代码
- 编译运行
(相较简便一些)
实验 一: 算术测试
一、实验目的
本实验的目的是让学生学习处理ActionEvent事件。
二、实验要求
编写一个算术测试小软件,用来训练小学生的算术能力。程序由3个类组成,其中Teacher类对象负责给出算术题目,并判断回答者的答案是否正确:ComputerFrame类对象提供的GUI界面看到题目并通过该GUI界面给出题目的答案;MailClass是主类。)。
三、程序代码
ComputerFrame .java
java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ComputerFrame extends JFrame {
private Teacher teacher;
private JTextField num1Field;
private JTextField operatorField;
private JTextField num2Field;
private JTextField answerField;
private JLabel resultLabel;
private String[] currentQuestion; // 保存当前题目的数组
public ComputerFrame(Teacher teacher) {
this.teacher = teacher;
setTitle("算术训练");
setSize(500, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 设置布局
setLayout(new FlowLayout());
// 显示题目框
num1Field = new JTextField(5);
num1Field.setEditable(false);
add(num1Field);
operatorField = new JTextField(2);
operatorField.setEditable(false);
add(operatorField);
num2Field = new JTextField(5);
num2Field.setEditable(false);
add(num2Field);
// 用户输入答案框
answerField = new JTextField(10);
add(answerField);
// 获取题目按钮
JButton getQuestionButton = new JButton("获取题目");
getQuestionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentQuestion = getRandomQuestion();
num1Field.setText(currentQuestion[0]);
operatorField.setText(currentQuestion[1]);
num2Field.setText(currentQuestion[2]);
resultLabel.setText(""); // 清空结果
}
});
add(getQuestionButton);
// 确认答案按钮
JButton checkAnswerButton = new JButton("确认答案");
checkAnswerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userAnswer = answerField.getText();
boolean isCorrect = teacher.checkAnswer(currentQuestion, userAnswer);
if (isCorrect) {
resultLabel.setText("你回答正确");
} else if (userAnswer.isEmpty() || !userAnswer.matches("-?\\d+")) {
resultLabel.setText("请输入数字字符");
} else {
resultLabel.setText("你回答错误");
}
}
});
add(checkAnswerButton);
// 结果显示框
resultLabel = new JLabel("");
add(resultLabel);
setVisible(true);
}
private String[] getRandomQuestion() {
// 根据简单模式或困难模式随机选择
int mode = JOptionPane.showOptionDialog(this, "请选择模式", "模式选择", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, new String[]{"简单模式", "困难模式"}, "简单模式");
if (mode == 0) {
return teacher.generateSimpleQuestion(); // 简单模式
} else {
return teacher.generateDifficultQuestion(); // 困难模式
}
}
}
Teacher .java
java
import java.util.Random;
public class Teacher {
private Random random;
public Teacher() {
random = new Random();
}
// 生成简单模式的加减题目
public String[] generateSimpleQuestion() {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
String operator = random.nextBoolean() ? "+" : "-";
return new String[]{String.valueOf(num1), operator, String.valueOf(num2)};
}
// 生成困难模式的乘除题目
public String[] generateDifficultQuestion() {
int num1 = random.nextInt(1, 100); // 除数不为0
int num2 = random.nextInt(1, 100);
String operator = random.nextBoolean() ? "*" : "/";
return new String[]{String.valueOf(num1), operator, String.valueOf(num2)};
}
// 判断答案是否正确
public boolean checkAnswer(String[] question, String answer) {
int num1 = Integer.parseInt(question[0]);
String operator = question[1];
int num2 = Integer.parseInt(question[2]);
int correctAnswer = 0;
switch (operator) {
case "+":
correctAnswer = num1 + num2;
break;
case "-":
correctAnswer = num1 - num2;
break;
case "*":
correctAnswer = num1 * num2;
break;
case "/":
correctAnswer = num1 / num2;
break;
}
try {
int userAnswer = Integer.parseInt(answer);
return userAnswer == correctAnswer;
} catch (NumberFormatException e) {
return false; // 输入的不是数字
}
}
}
MainClass.java
java
public class MainClass {
public static void main(String[] args) {
Teacher teacher = new Teacher(); // 创建老师对象
new ComputerFrame(teacher); // 创建并显示GUI界面
}
}
四、实验结果
(今日分享暂时到此为止啦!为不断努力的自己鼓鼓掌吧🥳。今日文案分享:所有失去的,会以另一种方式归来。)