Java实验完成
解决实验专用,勿扰谢谢 没问题!如果您有任何Java实验方面的问题,请随时向我提问。我会尽力帮助您解决问题。以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
帮我随便写一个Java程序
zqy173590939 发表于 2023-12-29 21:22
@FishC
帮我随便写一个Java程序
当然可以帮你写一个Java程序啦!那么,我们就来写一个简单的程序吧,让它输出一句话。
首先,我们需要创建一个Java类。在这个类中,我们会写一个方法来执行我们想要的操作(也就是输出一句话)。
我们命名这个类为 `HelloWorld`(你也可以根据自己的喜好来取名),代码如下:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
这段代码的逻辑很简单,就是调用 `System.out.println()` 方法来输出一句话 "Hello, World!"。当你运行这个程序时,控制台将会显示这句话。
希望这个例子能够帮到你入门Java编程!如果你有其他问题,随时告诉我哦!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2023-12-29 21:20
没问题!如果您有任何Java实验方面的问题,请随时向我提问。我会尽力帮助您解决问题。
以上回复来自 Fis ...
@FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("Equations saved successfully!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
为其添加检查习题功能 zqy173590939 发表于 2023-12-29 21:24
@FishC
package com.example.javafxmw;
import ga ...
要为程序添加检查习题的功能,可以在菜单中新增一个"Check"的选项,并在事件处理器中实现检查习题的逻辑。
首先,在`start`方法中,将以下代码添加到`Menu`对象的`MenuItem`列表中,用于创建并添加"Check"菜单项:
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
fileMenu.getItems().add(checkMenuItem);
然后,在`HelloApplication`类中添加一个新的方法`checkEquations()`,如下所示:
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
以上代码会遍历所有的算式,检查用户输入的答案是否正确,并根据结果将对应的算式框的背景颜色设置为绿色或红色。最后,弹出一个`Alert`对话框显示用户的正确答案数量和总题数。
现在,当用户点击"Check"菜单项时,即可检查习题的答案并给出相应的提示。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @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);
int num2 = Integer.parseInt(parts);
String operator = parts;
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);
int num2 = Integer.parseInt(parts);
String operator = parts;
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);
int b = Integer.parseInt(parts);
String operator = parts;
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();
}
}
这是我的完整项目代码,帮我编写总计报告的总结部分
@FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
menuBar.getMenus().add(checkMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("算式保存成功!!!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
}
修改代码,在点击读取算式的read按钮时,先清空当前GUI界面再展示读取的题目。 zqy173590939 发表于 2023-12-29 21:40
@FishC
package com.example.javafxmw;
import ga ...
在readMenuItem的事件处理方法中,添加以下代码:
flowPane.getChildren().clear();
这样在展示读取的题目之前,会先清空当前的GUI界面。修改后的代码如下:
readMenuItem.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
这样在点击读取按钮时,会先清空当前GUI界面,然后再将读取的题目展示出来。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
menuBar.getMenus().add(checkMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("算式保存成功!!!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
}
添加功能,点击check按钮后,界面下方才出现一个按钮View Accuracy,点击后显示饼状图展示正确率 zqy173590939 发表于 2023-12-29 21:51
@FishC
package com.example.javafxmw;
import...
要实现这个功能,你可以按照以下步骤进行操作:
1. 在你的`HelloApplication`类中添加一个`Button`对象和一个`PieChart`对象作为成员变量:
private Button viewAccuracyButton;
private PieChart accuracyChart;
2. 在`start`方法中创建并添加`viewAccuracyButton`到界面中:
viewAccuracyButton = new Button("View Accuracy");
flowPane.getChildren().add(viewAccuracyButton);
3. 为`viewAccuracyButton`设置点击事件处理器,在处理器中创建并显示`accuracyChart`:
viewAccuracyButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Stage chartStage = new Stage();
VBox chartBox = new VBox();
// 创建饼状图的数据列表
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Correct", correctAnswers),
new PieChart.Data("Incorrect", totalEquations - correctAnswers)
);
accuracyChart = new PieChart(pieChartData);
accuracyChart.setTitle("Accuracy Chart");
chartBox.getChildren().add(accuracyChart);
Scene scene = new Scene(chartBox, 400, 400);
chartStage.setScene(scene);
chartStage.show();
}
});
4. 在`checkEquations`方法中,更新正确答案数量和总题数之后,重新计算正确率,并将结果设置给饼状图的数据列表:
double accuracy = (double) correctAnswers / totalEquations * 100;
accuracyChart.getData().forEach(data -> {
if (data.getName().equals("Correct")) {
data.setPieValue(correctAnswers);
} else if (data.getName().equals("Incorrect")) {
data.setPieValue(totalEquations - correctAnswers);
}
});
现在,当你点击"Check"按钮后,界面下方会出现一个"View Accuracy"按钮。点击该按钮后,会弹出一个窗口,显示饼状图展示正确率。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2023-12-29 21:51
要实现这个功能,你可以按照以下步骤进行操作:
1. 在你的`HelloApplication`类中添加一个`Button`对象 ...
你提供的“在`checkEquations`方法中,更新正确答案数量和总题数之后”代码应该放在什么地方 zqy173590939 发表于 2023-12-29 21:56
你提供的“在`checkEquations`方法中,更新正确答案数 ...
抱歉,我之前提供的代码放错位置了。把更新正确答案数量和总题数以及计算正确率的代码放在`checkEquations`方法的末尾。下面是完整的`checkEquations`方法的修改版本:
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
// 更新正确答案数量和总题数的饼状图数据
double accuracy = (double) correctAnswers / totalEquations * 100;
accuracyChart.getData().forEach(data -> {
if (data.getName().equals("Correct")) {
data.setPieValue(correctAnswers);
} else if (data.getName().equals("Incorrect")) {
data.setPieValue(totalEquations - correctAnswers);
}
});
}
这样,在点击"Check"按钮后,正确率的饼状图数据会被更新,并显示在点击"View Accuracy"按钮后弹出的窗口中。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
menuBar.getMenus().add(checkMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("算式保存成功!!!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
}
添加功能,点击check按钮后,界面最下方才出现一个新的按钮View Accuracy,点击View Accuracy后显示一个饼状图展示正确率,给出完整代码 zqy173590939 发表于 2023-12-29 21:59
@FishC
package com.example.javafxmw;
import...
好的,下面是完整的修改后的代码:
package com.example.javafxmw;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
MenuItem viewAccuracyMenuItem = new MenuItem("View Accuracy");
viewAccuracyMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 显示正确率饼状图
showAccuracyChart();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().add(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu, saveMenu, readMenu, checkMenu, viewAccuracyMenuItem);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
private void saveEquations() {
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 保存到数据库
// ...
}
}
}
private void displayAllEquationsFromDatabase() {
// 从数据库中读取习题并显示在GUI中
// ...
}
private void checkEquations() {
int totalEquations = 0; // 总题数
correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void showAccuracyChart() {
double accuracy = (double) correctAnswers / flowPane.getChildren().size();
Stage chartStage = new Stage();
chartStage.setTitle("正确率");
PieChart pieChart = new PieChart();
PieChart.Data correctData = new PieChart.Data("正确", correctAnswers);
PieChart.Data incorrectData = new PieChart.Data("错误", flowPane.getChildren().size() - correctAnswers);
pieChart.getData().addAll(correctData, incorrectData);
HBox hbox = new HBox(pieChart);
hbox.setPadding(new Insets(10));
Scene scene = new Scene(hbox, 400, 400);
chartStage.setScene(scene);
chartStage.show();
}
}
在代码中添加了`viewAccuracyMenuItem`菜单项和`showAccuracyChart()`方法。当用户点击"View Accuracy"菜单项后,会弹出一个新窗口,显示正确率的饼状图。饼状图上有两部分,分别表示答对和答错的题目数量。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
private Label scoreLabel;
private int score = 0;
private int correctAnswers = 0;
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
menuBar.getMenus().add(checkMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("算式保存成功!!!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
}
添加功能,点击check按钮后,界面最下方才出现一个新的按钮View Accuracy,点击View Accuracy后显示一个饼状图展示正确率 zqy173590939 发表于 2023-12-29 22:04
@FishC
package com.example.javafxmw;
import...
要实现该功能,你需要引入额外的库来绘制饼状图。这里推荐使用JavaFX自带的图表库`javafx.scene.chart`来绘制饼状图。
首先,在你的`HelloApplication`类中添加一个新的菜单项和按钮:
MenuItem viewAccuracyMenuItem = new MenuItem("View Accuracy");
Button viewAccuracyButton = new Button("View Accuracy");
然后,在`start`方法中的`Menu`和`MenuBar`的相关代码处添加`viewAccuracyMenuItem`:
checkMenu.getItems().add(viewAccuracyMenuItem);
接着,在`borderPane`中添加一个`HBox`来包含`viewAccuracyButton`,并为其添加点击事件监听器:
HBox bottomBox = new HBox(10, viewAccuracyButton);
borderPane.setBottom(bottomBox);
// View Accuracy按钮的点击事件监听器
viewAccuracyButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 显示饼状图
showAccuracyChart();
}
});
最后,实现一个`showAccuracyChart`方法,用来显示饼状图:
private void showAccuracyChart() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
correctAnswers++;
}
}
}
}
// 创建一个饼状图数据
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Correct", correctAnswers),
new PieChart.Data("Incorrect", totalEquations - correctAnswers)
);
// 创建饼状图
PieChart chart = new PieChart(pieChartData);
chart.setTitle("Accuracy Chart");
// 创建一个新的Stage来显示饼状图
Stage chartStage = new Stage();
chartStage.setScene(new Scene(chart, 600, 400));
chartStage.show();
}
现在,当用户点击"View Accuracy"按钮时,将会显示一个新的窗口,里面包含一个饼状图,用来展示用户答题的正确率。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
package com.example.javafxmw;
import gaijin3.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloApplication extends Application
{
// 定义变量
Button viewAccuracyButton = new Button("View Accuracy");
FlowPane flowPane = new FlowPane();
private int maxID = 0;
@Override
public void start(Stage stage) throws IOException {
MenuItem generateMenuItem = new MenuItem("Generate");
// 事件处理器代码
generateMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 创建一个警示对话框
TextInputDialog dialog = new TextInputDialog("5");
dialog.setTitle("请输入算式数量");
dialog.setHeaderText(null);
dialog.setContentText("算式数量:");
// 显示对话框,并获取用户输入的算式数量
Optional<String> result = dialog.showAndWait();
if (result.isPresent())
{
int n = Integer.parseInt(result.get());
// 生成算式
EquationCollection equationCollection = generateEquations(n);
// 清空之前的内容
flowPane.getChildren().clear();
// 遍历算式集合并显示在GUI中
for (IEquation equation : equationCollection)
{
maxID++; // id自增
Label idLabel = new Label(String.valueOf(maxID)); // 获取当前最大ID,并将新的算式ID设为其加1
Label equationLabel = new Label(equation.toString());
TextField answerField = new TextField(); // 创建一个新的文本框
HBox equationBox = new HBox(10, idLabel, new Label("、"), equationLabel, answerField); // 将算式ID标签、顿号、算式标签和文本框放入水平布局
flowPane.getChildren().add(equationBox); // 将水平布局添加到 flowPane 中
}
// 连接数据库
connectDatabase();
}
}
});
MenuItem saveMenuItem = new MenuItem("Save");
saveMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
saveEquations();
}
});
MenuItem readMenuItem = new MenuItem("Read");
readMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
flowPane.getChildren().clear(); // 清空当前GUI界面
showAlert("习题已读取");
displayAllEquationsFromDatabase();
}
});
MenuItem checkMenuItem = new MenuItem("Check");
checkMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 检查习题
checkEquations();
}
});
MenuItem viewAccuracyMenuItem = new MenuItem("View Accuracy");
// View Accuracy按钮的点击事件监听器
viewAccuracyButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 显示饼状图
showAccuracyChart();
}
});
Menu fileMenu = new Menu("File");
Menu saveMenu = new Menu("SaveEquation");
Menu readMenu = new Menu("ReadEquation");
Menu checkMenu = new Menu("CheckEquation");
fileMenu.getItems().addAll(generateMenuItem);
saveMenu.getItems().add(saveMenuItem);
readMenu.getItems().add(readMenuItem);
checkMenu.getItems().add(checkMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
menuBar.getMenus().add(saveMenu);
menuBar.getMenus().add(readMenu);
menuBar.getMenus().add(checkMenu);
flowPane.setHgap(50);
flowPane.setVgap(30);
flowPane.setPadding(new Insets(20));
flowPane.setPrefSize(800, 560); // 设置 FlowPane 的大小
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(flowPane);
HBox bottomBox = new HBox(10, viewAccuracyButton);
borderPane.setBottom(bottomBox);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("口算习题薄");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
// 生成算式的方法
public EquationCollection generateEquations(int n) {
EquationCollection equationCollection = new EquationCollection();
EquationCheckerOfRange equationChecker = new EquationCheckerOfRange(0, 100);
equationCollection.generate(n, equationChecker);
return equationCollection;
}
// 连接数据库的方法
public void connectDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
// 查询最大ID值
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM equationtable");
if (rs.next()) {
maxID = rs.getInt(1);
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void saveEquations() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label idLabel = (Label) equationBox.getChildren().get(0); // 获取算式ID标签
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
int answer = 0;
if (!answerText.isEmpty()) {
answer = Integer.parseInt(answerText);
}
// 计算正确结果
int result = calculateResult(equation);
// 保存到数据库
String sql = "INSERT INTO equationtable (id, Operand1, Operator, Operand2, Result) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(idLabel.getText())); // 将算式ID插入到数据库中
pstmt.setInt(2, getOperand1(equation));
pstmt.setString(3, getOperator(equation));
pstmt.setInt(4, getOperand2(equation));
pstmt.setInt(5, result);
pstmt.executeUpdate();
}
}
System.out.println("算式保存成功!!!");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private int calculateResult(String equation) {
String[] parts = equation.split(" ");
int operand1 = Integer.parseInt(parts);
int operand2 = Integer.parseInt(parts);
String operator = parts;
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*": //此处预设乘法和除法便于以后相应的添加功能
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
return result;
}
private int getOperand1(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private String getOperator(String equation) {
String[] parts = equation.split(" ");
return parts;
}
private int getOperand2(String equation) {
String[] parts = equation.split(" ");
return Integer.parseInt(parts);
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("警示对话框");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void displayAllEquationsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/rjgzdatabase?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
"root", "Zqy173590939");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM equationtable");
if (!rs.next()) {
showAlert("当前题库为空");
} else {
do {
int id = rs.getInt("id");
String equation = id + "、 " + rs.getInt("Operand1") + " " + rs.getString("Operator") + " " +
rs.getInt("Operand2") + " = " + rs.getInt("Result");
Label equationLabel = new Label(equation);
flowPane.getChildren().add(equationLabel);
} while (rs.next());
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
private void checkEquations() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
equationBox.setStyle("-fx-background-color: #99ff99"); // 设置背景色为绿色
correctAnswers++;
} else { // 答案错误
equationBox.setStyle("-fx-background-color: #ff9999"); // 设置背景色为红色
}
}
}
}
showAlert("您一共答对了 " + correctAnswers + " 题,总共提交了 " + totalEquations + " 题。");
}
private void showAccuracyChart() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
correctAnswers++;
}
}
}
}
// 创建一个饼状图数据
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Correct", correctAnswers),
new PieChart.Data("Incorrect", totalEquations - correctAnswers)
);
// 创建饼状图
PieChart chart = new PieChart(pieChartData);
chart.setTitle("Accuracy Chart");
// 创建一个新的Stage来显示饼状图
Stage chartStage = new Stage();
chartStage.setScene(new Scene(chart, 600, 400));
chartStage.show();
}
}
修改代码,饼图显示正确率时,同时显示出每部分占的百分比 zqy173590939 发表于 2023-12-29 22:26
@FishC
package com.example.javafxmw;
import...
要同时显示饼图每部分的百分比,可以通过设置饼状图的标签生成器(label generator)来实现。下面是修改后的代码:
private void showAccuracyChart() {
int totalEquations = 0; // 总题数
int correctAnswers = 0; // 答对题数
for (Node node : flowPane.getChildren()) {
if (node instanceof HBox) {
HBox equationBox = (HBox) node;
Label equationLabel = (Label) equationBox.getChildren().get(2);
TextField answerField = (TextField) equationBox.getChildren().get(3);
String equation = equationLabel.getText();
String answerText = answerField.getText();
// 检查答案字段是否为空
if (!answerText.isEmpty()) {
totalEquations++;
int userAnswer = Integer.parseInt(answerText);
int result = calculateResult(equation);
if (userAnswer == result) { // 答案正确
correctAnswers++;
}
}
}
}
// 计算百分比
double correctPercentage = (double) correctAnswers / totalEquations;
double incorrectPercentage = 1 - correctPercentage;
// 创建一个饼状图数据
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Correct", correctPercentage),
new PieChart.Data("Incorrect", incorrectPercentage)
);
// 设置饼状图标签生成器,显示每部分的百分比
PieChart chart = new PieChart(pieChartData) {
@Override
protected javafx.scene.chart.PieChart.Data getDataItem(int index) {
PieChart.Data data = super.getDataItem(index);
String percentage = String.format("%.2f%%", data.getPieValue() * 100);
data.setName(data.getName() + " (" + percentage + ")");
return data;
}
};
chart.setTitle("Accuracy Chart");
// 创建一个新的Stage来显示饼状图
Stage chartStage = new Stage();
chartStage.setScene(new Scene(chart, 600, 400));
chartStage.show();
}
在代码中,首先计算出正确率的百分比,然后创建饼状图数据,并将每部分的名称后面附加上对应的百分比信息。最后在饼状图中设置了一个标签生成器,用于显示每部分的名称和百分比。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @FishC
package com.example.javafxmw;
import javafx.scene.control.*;
import shiyan4.EquationChecker;
import shiyan4.EquationCheckerOfRange;
import shiyan4.EquationCollection;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import shiyan4.IEqualtion;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
import java.util.Scanner;
public class HelloApplication extends Application { // 声明HelloApplication类,继承自Application类
private FlowPane flowPane; // 用于显示算术表达式和输入字段的FlowPane
private TextField[] resultFields; // 用于存储结果输入字段的数组
private int TopicNumber;
// MySQL 5.x版本
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
// 注意数据库的名字要根据实际情况来,这里数据库名字是app
static final String DB_URL = "jdbc:mysql://localhost:3306/shiyan8";
static final String URL_EXTRA_PARA = "?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "";
@Override
public void start(Stage stage) throws IOException { // 覆盖重写Application类的start方法,并声明可能抛出IOException异常
MenuItem generateMenuItem = new MenuItem("产生习题"); // 创建一个名为"Generate"的菜单项
MenuItem saveMenuItem = new MenuItem("保存习题"); // 创建一个名为"Generate"的菜单项
MenuItem readMenuItem = new MenuItem("读取习题"); // 创建一个名为"Generate"的菜单项
MenuItem checkMenuItem = new MenuItem("检查习题"); // 创建一个名为"Generate"的菜单项
Menu fileMenu = new Menu("菜单"); // 创建一个名为"File"的菜单
fileMenu.getItems().addAll(generateMenuItem); // 将generateMenuItem菜单项添加到fileMenu菜单中
fileMenu.getItems().addAll(saveMenuItem);
fileMenu.getItems().addAll(readMenuItem);
fileMenu.getItems().addAll(checkMenuItem);
MenuBar menuBar = new MenuBar(); // 创建一个菜单栏
menuBar.getMenus().addAll(fileMenu); // 将fileMenu菜单添加到菜单栏中
FlowPane flowPane = new FlowPane(); // 创建一个FlowPane布局
flowPane.setHgap(20); // 设置水平间距为50
flowPane.setVgap(30); // 设置垂直间距为30
flowPane.setPadding(new Insets(30)); // 设置内边距为20
BorderPane borderPane = new BorderPane(); // 创建一个BorderPane布局
borderPane.setTop(menuBar); // 将菜单栏放置在顶部
borderPane.setCenter(flowPane); // 将FlowPane布局放置在中间
Scene scene = new Scene(borderPane, 800, 640); // 创建一个场景,将borderPane布局放置在其中,设置宽度为800,高度为640
stage.setTitle("口算习题薄"); // 设置舞台的标题为"口算习题薄"
stage.setScene(scene); // 将场景设置到舞台上
stage.show(); // 显示舞台
EquationChecker checker = new EquationCheckerOfRange(0, 100); // 创建一个范围检查器对象,用于检查生成的方程是否在指定范围内
EquationCollection equa = new EquationCollection(); // 创建一个方程集合对象,用于存储生成的方程
//产生习题
generateMenuItem.setOnAction(actionEvent -> {
//题目数量
TextInputDialog textInputDialog = new TextInputDialog("");
textInputDialog.setTitle("输入生成题目数量");
textInputDialog.setHeaderText(null);
textInputDialog.setContentText("数量:");
Optional<String> textResult = textInputDialog.showAndWait();
textResult.ifPresent(result -> {
TopicNumber = Integer.parseInt(result);
});
equa.generate(TopicNumber, checker); // 调用方程集合对象的generate方法,生成指定数量的方程,并使用范围检查器进行检查
flowPane.getChildren().clear(); // 清空FlowPane
resultFields = new TextField; // 初始化resultFields数组以存储5个输入字段
int i = 0;
for (IEqualtion equation : equa) {
String arithmeticExpression = i + 1 + "、" + equation.getNum1() + "" + equation.getOperator() + "" + equation.getNum2() + "="; // 生成一个随机算术表达式
Label expressionLabel = new Label(arithmeticExpression); // 创建一个标签来显示算术表达式
resultFields = new TextField(); // 创建一个文本字段用于输入结果
resultFields.setPrefColumnCount(3); // 设置文本字段的列数为5,以容纳5个字符
flowPane.getChildren().addAll(expressionLabel, resultFields); // 将标签和文本字段添加到FlowPane
i++;
}
});
//检查习题
checkMenuItem.setOnAction(actionEvent -> {
int i = 0;
int score = 0;
int[] fault = new int;
for (IEqualtion equation : equa) {
if (!resultFields.getText().isEmpty()) {
int result = Integer.parseInt(resultFields.getText()); // 从输入字段解析结果
if (equation.calculate() == result) {
score++;
} else {
fault = i + 1;
}
} else {
fault = i + 1;
}
i++;
}
Alert alert = new Alert(Alert.AlertType.INFORMATION); // 创建一个信息警报
StringBuilder FaultAssemble = new StringBuilder();
for (int j = 0; j < TopicNumber; j++) {
if (fault != 0) {
FaultAssemble.append(fault + ", ");
}
}
alert.setContentText("你的成绩是: " + score + "\n" + "你的错题是: " + FaultAssemble.toString()); // 设置警报的内容
alert.show(); // 显示警报
});
//保存习题
saveMenuItem.setOnAction(actionEvent -> {
Connection conn = null;
Statement stmt = null;
try {
System.out.println("加载类从而注册驱动");
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL + URL_EXTRA_PARA, USER, PASS);
DatabaseMetaData dbMeta = conn.getMetaData();
System.out.println("当前用户:" + dbMeta.getUserName());
System.out.println("驱动版本:" + dbMeta.getDriverVersion());
System.out.println("MySQL版本:" + dbMeta.getDatabaseProductVersion());
stmt = conn.createStatement();
int i = 1;
for (IEqualtion equaltion : equa) {
String exer = equaltion.getNum1() + " " + equaltion.getOperator() + " " + equaltion.getNum2() + " =";
String sql = "INSERT INTO topic (serial, exercise, answer) VALUES(" + i + ",' "+ exer +" '," + equaltion.calculate() + ")";
stmt.executeUpdate(sql);
i++;
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
});
}
public static void main(String[] args) { // 主方法
launch(); // 启动JavaFX应用程序
}
}
以上是实验代码,出现了以下问题,给出解决方案
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:421)
at java.base/java.lang.Class.forName(Class.java:412)
at com.example.javafxmw/com.example.javafxmw.HelloApplication.lambda$start$3(HelloApplication.java:139)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:459)
at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1385)
at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.lambda$createChildren$12(ContextMenuContent.java:1338)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1583)