From 3bb158eccdabe93c7803aba5299034e3c27b4def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=A0=E6=B1=9F?= <15547749+cyj-050209@user.noreply.gitee.com> Date: Sat, 11 Oct 2025 15:30:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=89=80=E6=9C=89=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E4=B8=8E=E5=8A=9F=E8=83=BD1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/users.json | 2 +- .../java/mathlearning/ui/SelTestFrame.java | 40 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/data/users.json b/data/users.json index 2ada8b1..3f0ec0d 100644 --- a/data/users.json +++ b/data/users.json @@ -5,5 +5,5 @@ "registrationDate" : [ 2025, 10, 10, 11, 7, 5, 853200500 ], "verificationCode" : "688201", "verified" : true, - "type" : "小学" + "type" : "初中" } ] \ No newline at end of file diff --git a/src/main/java/mathlearning/ui/SelTestFrame.java b/src/main/java/mathlearning/ui/SelTestFrame.java index cc7ae84..851002d 100644 --- a/src/main/java/mathlearning/ui/SelTestFrame.java +++ b/src/main/java/mathlearning/ui/SelTestFrame.java @@ -13,7 +13,7 @@ public class SelTestFrame extends JFrame { private int questionNum; private int currentQuestionDex; - private String[] answers; + private String[] myAnswers; private JLabel titleLabel; private JButton optionA; @@ -30,7 +30,7 @@ public class SelTestFrame extends JFrame { this.userService = userService; this.questionNum = num; this.currentQuestionDex = 0; - this.answers = new String[questionNum]; + this.myAnswers = new String[questionNum]; InitUI(); addWindowListener(new java.awt.event.WindowAdapter() { @Override @@ -158,19 +158,19 @@ public class SelTestFrame extends JFrame { switch (answer) { case "A": optionA.setBackground(Color.GREEN); - answers[currentQuestionDex] = answer; + myAnswers[currentQuestionDex] = answer; break; case "B": optionB.setBackground(Color.GREEN); - answers[currentQuestionDex] = answer; + myAnswers[currentQuestionDex] = answer; break; case "C": optionC.setBackground(Color.GREEN); - answers[currentQuestionDex] = answer; + myAnswers[currentQuestionDex] = answer; break; case "D": optionD.setBackground(Color.GREEN); - answers[currentQuestionDex] = answer; + myAnswers[currentQuestionDex] = answer; break; } @@ -187,11 +187,11 @@ public class SelTestFrame extends JFrame { if (currentQuestionDex > 0) { currentQuestionDex--; updateTitleLabel(); - if (answers[currentQuestionDex] == null) { + if (myAnswers[currentQuestionDex] == null) { resetButtonColors(); } else { - saveAnswer(answers[currentQuestionDex]); + saveAnswer(myAnswers[currentQuestionDex]); } updateNavigationButtons(); @@ -202,10 +202,10 @@ public class SelTestFrame extends JFrame { if (currentQuestionDex < questionNum - 1) { currentQuestionDex++; updateTitleLabel(); - if (answers[currentQuestionDex] == null) { + if (myAnswers[currentQuestionDex] == null) { resetButtonColors(); } else { - saveAnswer(answers[currentQuestionDex]); + saveAnswer(myAnswers[currentQuestionDex]); } updateNavigationButtons(); @@ -215,13 +215,16 @@ public class SelTestFrame extends JFrame { private void goToSubmit() { int confirm = JOptionPane.showConfirmDialog(SelTestFrame.this, "你确定要提交试卷吗?", "提示", JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { - int score = 0; + int correctCount = 0; + + double accuracy = Math.round((double) correctCount / myAnswers.length * 10000) / 100.0; + double score = Math.round((double) correctCount * 100 / myAnswers.length * 10) / 10.0; // 弹出分数结果 String[] options = {"返回主菜单", "继续做题"}; int choice = JOptionPane.showOptionDialog( SelTestFrame.this, - "您的得分是: " + score + "分", + "您答对了" + score + "道题,正确率为" + String.format("%.2f", accuracy) + "\n您的得分是: " + score + "分", "测试结果", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, @@ -275,4 +278,17 @@ public class SelTestFrame extends JFrame { dispose(); } } + + private int getScore(String[] answers, String[] myAnswers) { + if (answers == null || myAnswers == null){ + return 0; + } + int score = 0; + for (int i = 0; i < answers.length; i++) { + if (answers[i] != null && answers[i].equals(myAnswers[i])){ + score++; + } + } + return score; + } }