|
发表于 2023-12-29 21:29:09
|
显示全部楼层
@FishCpackage com.example.javafxmw;
import Math4.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.*;
import java.util.ArrayList;
import java.util.Optional;
public class HelloApplication extends Application {
private ArrayList<Label> labels = new ArrayList<>(); // 用于存储算式标签
private ArrayList<TextField> fields = new ArrayList<>(); // 用于存储结果输入框
private FlowPane flowPane; // 用于显示算式和输入框
@Override
public void start(Stage stage) {
//创建一个子菜单项
MenuItem generateMenuItem = new MenuItem("生成算式");
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 点击菜单项生成算式
generate1();
}
});
Menu fileMenu = new Menu("文件");
fileMenu.getItems().addAll(generateMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
//设置子节点的间距
flowPane = new FlowPane();
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
VBox vBox = new VBox();
vBox.setSpacing(10);
//创建检查得分按钮
Button checkButton = new Button("检查得分");
checkButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 点击检查按钮计算得分
check();
}
});
vBox.getChildren().add(checkButton);
//创建保存按钮
Button saveButton = new Button("保存");
saveButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 点击保存按钮保存算式
saveEquation();
}
});
vBox.getChildren().add(saveButton);
//查看本次正确率
Button rateButton = new Button("本次正确率");
rateButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent actionEvent) {
//调用查看饼状图的方法
showThisTimePieChart();
}
});
//将按钮添加到VBox中
vBox.getChildren().add(rateButton);
//查看总正确率
Button totalRateButton = new Button("总正确率");
totalRateButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent actionEvent) {
showTotalPieChart();
}
});
vBox.getChildren().add(totalRateButton);
//查看所有题目
Button showAllButton = new Button("查看所有题目");
showAllButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
showAllEquation();
}
});
vBox.getChildren().add(showAllButton);
//查看错题按钮
Button showErrorButton = new Button("查看错题");
showErrorButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent actionEvent) {
showErrorEquation();
}
});
vBox.getChildren().add(showErrorButton);
// 创建一个BorderPane布局
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
borderPane.setRight(vBox);
Scene scene = new Scene(borderPane , 800 , 640);
stage.setTitle("算术题集");
stage.setScene(scene);
stage.show();
}
// 生成道算式
private void generate1() {
// 清空界面上原有的 Label 和 TextField
flowPane.getChildren().clear();
labels.clear();
fields.clear();
// 创建一个 TextInputDialog 对象
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("生成算术式");
dialog.setHeaderText(null);
dialog.setContentText("请输入算术式的数量:");
// 获取用户输入的算术式数量
Optional<String> result = dialog.showAndWait();
int equationCount = 0;
if(result.isPresent()) {
try {
equationCount = Integer.parseInt(result.get());
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
// 设置算术式答案的范围
EquationCheckerOfRange checker = new EquationCheckerOfRange(0 , 100);
EquationCollection collection = new EquationCollection();
// 生成算术式
collection.generate(equationCount , checker);
//遍历生成的算术式对象
for(IEqualtion equation : collection) {
Label label = new Label(equation.toString());
TextField field = new TextField();
//给TextField添加KeyEvent事件处理器,点击空格查看得分
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// 按下回车键时计算得分
if(event.getCode().toString().equals("ENTER")) {
check();
}
}
});
labels.add(label);
fields.add(field);
flowPane.getChildren().addAll(label , field);
}
}
// 计算得分并弹出提示框显示
private void check() {
//调用方法计算答对题目的数量
int score = thisTimeCorrectNum();
// 文本提示框
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("你的得分是:" + score);
alert.showAndWait();
}
// 保存算术式和得分到数据库
private void saveEquation() {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection(); // 获取数据库连接
// 清空表中的内容
String clearSql = "TRUNCATE TABLE thistimeequation";
statement = connection.prepareStatement(clearSql);
statement.executeUpdate();
// 创建插入语句
String sql = "INSERT INTO thistimeequation (num1, num2, operator, result) VALUES ( ?, ?, ?, ?)";
statement = connection.prepareStatement(sql);
// 循环插入每个算术式
for(int i = 0 ; i < labels.size() ; i++) {
Label label = labels.get(i);
String[] parts = label.getText().split(" ");
int num1 = Integer.parseInt(parts[0]);
int num2 = Integer.parseInt(parts[2]);
String operator = parts[1];
int result = operator.equals("+") ? num1 + num2 : num1 - num2;
// 设置参数并执行插入
statement.setInt( 1 , num1);
statement.setInt(2 , num2);
statement.setString(3 , operator);
statement.setInt(4 , result);
statement.executeUpdate();
}
//创建一个新表,永久存放生成的算术式,id自增,设主键,列名为id,num1,num2,operator,result,inputresult
String sql1 = "INSERT INTO totalequation (num1, num2, operator, result, inputresult) VALUES (?, ?, ?, ?, ?)";
statement = connection.prepareStatement(sql1);
for(int i = 0 ; i < labels.size() ; i++) {
// 获取算术式和用户输入的结果
Label label = labels.get(i);
TextField field = fields.get(i);
String[] parts = label.getText().split(" ");
int num1 = Integer.parseInt(parts[0]);
int num2 = Integer.parseInt(parts[2]);
String operator = parts[1];
int result = operator.equals("+") ? num1 + num2 : num1 - num2;
int inputResult = Integer.parseInt(field.getText());
// 设置参数并执行插入
statement.setInt(1 , num1);
statement.setInt( 2, num2);
statement.setString(3 , operator);
statement.setInt( 4, result);
statement.setInt(5 , inputResult);
statement.executeUpdate();
}
//提示保存成功
Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
successAlert.setContentText("保存成功!");
successAlert.showAndWait();
} catch (SQLException e) {
e.printStackTrace();
// 提示保存失败
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
errorAlert.setContentText("保存失败!");
errorAlert.showAndWait();
} finally {
// 关闭连接和语句
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
// 连接数据库
private Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/equation";
String username = "root";
String password = "1542";
return DriverManager.getConnection(url , username , password);
}
private void showThisTimePieChart() {
if(labels.isEmpty()){
// 提示没有数据
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("没有数据,无法显示图表!");
alert.showAndWait();
return;
}
Stage chartStage = new Stage();
chartStage.setTitle("答案正确率饼状图");
PieChart pieChart = new PieChart();
pieChart.setTitle("本次答题正确率");
//遍历算术式标签得到总数
int totalQuestions = labels.size();
//答对的题目的数量,调用计算正确答对题目数量的方法
int correctAnswers = thisTimeCorrectNum();
double correctRate = (double) correctAnswers / totalQuestions;
// 创建表示正确率和错误率的数据
PieChart.Data correctData = new PieChart.Data("正确 " + (correctRate * 100) + "%", correctRate);
PieChart.Data incorrectData = new PieChart.Data("错误 " + ((1 - correctRate) * 100) + "%", 1 - correctRate);
// 将数据添加到饼图中
pieChart.getData().add(correctData);
pieChart.getData().add(incorrectData);
//创建一个 Scene 对象,将 pieChart 设置为其中的内容
Scene scene = new Scene(pieChart, 800, 600);
chartStage.setScene(scene);
chartStage.show();
}
public void showTotalPieChart() {
// 创建一个新的 Stage 对象来显示总的饼状图
Stage chartStage = new Stage();
chartStage.setTitle("所有题目正确率饼状图");
// 创建一个 PieChart 对象
PieChart pieChart = new PieChart();
pieChart.setTitle("总答题正确率");
// 计算总的答题正确率
int totalEquation = 0;
int correctNum = totalCorrectNum();
//得出数据库中一共有多少题目
try {
// 获取数据库连接
Connection connection = getConnection();
// 创建查询语句,total指定COUNT()函数返回的计数结果的列名
// COUNT(*)表示统计表中的行数
String querySql = "SELECT COUNT(*) AS total FROM totalequation";
// 创建Statement对象执行查询语句
Statement statement = connection.createStatement();
//获取执行的结果
ResultSet resultSet = statement.executeQuery(querySql);
// 获取查询结果
if (resultSet.next()) {
totalEquation = resultSet.getInt("total");
}
// 关闭ResultSet和Statement对象
resultSet.close();
statement.close();
// 关闭数据库连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
//计算正确率
double TotalCorrectRate = (double) correctNum / totalEquation;
// 创建表示正确率和错误率的数据
PieChart.Data correctData = new PieChart.Data("正确 " + (TotalCorrectRate * 100) + "%", TotalCorrectRate);
PieChart.Data incorrectData = new PieChart.Data("错误 " + ((1 - TotalCorrectRate) * 100) + "%", 1 - TotalCorrectRate);
// 将数据添加到饼图中
pieChart.getData().add(correctData);
pieChart.getData().add(incorrectData);
//创建一个 Scene 对象,将 pieChart 设置为其中的内容
Scene scene = new Scene(pieChart, 800, 600);
chartStage.setScene(scene);
chartStage.show();
}
//计算答对题目数量的方法
public int thisTimeCorrectNum(){
int correctEquation = 0;
// 计算正确答案的数量
for(int i = 0 ; i < labels.size() ; i++) {
//遍历标签和文本框,
Label label = labels.get(i);
TextField field = fields.get(i);
//分割算术式,统计正确的数量
String[] parts = label.getText().split(" ");
int a = Integer.parseInt(parts[0]);
int b = Integer.parseInt(parts[2]);
String operator = parts[1];
int actualResult = operator.equals("+") ? a + b : a - b;
if (field.getText().equals(String.valueOf(actualResult))) {
correctEquation++;
}
}
return correctEquation;
}
//计算数据库中所有题目的正确数量
public int totalCorrectNum() {
int totalCorrectNum = 0;
try {
// 获取数据库连接
Connection connection = getConnection();
// 创建查询语句
String querySql = "SELECT result, inputresult FROM totalequation";
// 创建Statement对象执行查询语句
Statement statement = connection.createStatement();
// 执行查询语句并获取结果集
ResultSet resultSet = statement.executeQuery(querySql);
// 遍历结果集中所有的算术式
while (resultSet.next()) {
int result = resultSet.getInt("result");
int inputResult = resultSet.getInt("inputresult");
// 比较result和inputResult,如果相等则答案正确
if (result == inputResult) {
totalCorrectNum++;
}
}
// 关闭ResultSet和Statement对象
resultSet.close();
statement.close();
// 关闭数据库连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return totalCorrectNum;
}
private void showAllEquation() {
ArrayList<String> equations = retrieveAllEquations();
//清除原有内容
flowPane.getChildren().clear();
//将算术式添加到界面上
for (String equation : equations) {
Label label = new Label(equation);
labels.add(label);
flowPane.getChildren().addAll(label);
}
}
//检索totalequation表中所有的算术式
private ArrayList<String> retrieveAllEquations() {
ArrayList<String> equations = new ArrayList<>();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 连接数据库
connection = getConnection();
// 执行查询
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM totalequation");
// 遍历结果集,将每个算术式拼接成字符串并添加到列表中
while (resultSet.next()) {
int num1 = resultSet.getInt("num1");
String operator = resultSet.getString("operator");
int num2 = resultSet.getInt("num2");
int result = resultSet.getInt("result");
String equation = num1 + " " + operator + " " + num2 + " = " + result;
equations.add(equation);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和 statement
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return equations;
}
//查看错题
private void showErrorEquation() {
ArrayList<String> ErrorEquations = retrieveAllErrorEquations();
//清除原有内容
flowPane.getChildren().clear();
//将算术式添加到界面上
for (String equation : ErrorEquations) {
Label label = new Label(equation);
labels.add(label);
flowPane.getChildren().addAll(label);
}
}
private ArrayList<String> retrieveAllErrorEquations() {
ArrayList<String> ErrorEquations = new ArrayList<>();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 连接数据库
connection = getConnection();
// 执行查询
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM totalequation");
// 遍历结果集,将每个算术式拼接成字符串并添加到列表中
while (resultSet.next()) {
int num1 = resultSet.getInt("num1");
String operator = resultSet.getString("operator");
int num2 = resultSet.getInt("num2");
int result = resultSet.getInt("result");
int inputResult = resultSet.getInt("inputresult");
String equation = num1 + " " + operator + " " + num2 + " = " + result;
if(result != inputResult) {
ErrorEquations.add(equation);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和 statement
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return ErrorEquations;
}
public static void main(String[] args) {
launch();
}
}
这是我的完整项目代码,帮我编写总计报告的总结部分
|
|