Java Swing开发Jeopardy游戏JButton计分始终为0问题排查
类Jeopardy答题游戏分值无法累加问题修复
问题现象
- 每行题目按钮对应100-500的分值梯度,预期流程为:点击选题按钮记录对应分值,提交答案时校验正确性,答对则将对应分值累加到玩家总分并更新GUI分数标签
- 实际运行时,答案校验正确的前提下分数始终显示为0,无法正常累计
问题代码
public class GUIWindow implements ActionListener{ Players p1 = new Players(); private int p1Score = 0; private int p2Score = 0; public void actionPerformed(ActionEvent e){ int points = 0; for(int i = 0; i < 5; i++){ if(e.getSource() == boardButtonsRow1[i]){ points = 500; }else if(e.getSource() == boardButtonsRow2[i]){ points = 400; }else if(e.getSource() == boardButtonsRow3[i]){ points = 300; }else if(e.getSource() == boardButtonsRow4[i]){ points = 200; }else if(e.getSource() == boardButtonsRow2[i]){ points = 100; } } if(e.getSource() == submitBtn){ String answer = answerEntry.getText(); if(answer.equalsIgnoreCase(q1.getAnswer())){ System.out.println("Correct"); System.out.println(points); p1Score = p1Score + points; p1.setPoints(p1Score); showP1ScoreLbl.setText(""+p1.getScore()); } }
根因分析
代码存在3个明确逻辑错误:
- 变量作用域错误:
points被定义为actionPerformed方法的局部变量,每次触发GUI事件(不管是点击题目按钮还是提交按钮),方法执行时都会把points重置为0。点击题目时确实给当次方法调用里的points赋了值,但点击提交触发的是全新的方法调用,之前的赋值已经被销毁,拿到的永远是初始值0。 - 分支判断笔误:100分对应的应该是第5行按钮,代码里重复写了
boardButtonsRow2[i],和400分的判断条件完全重复,就算作用域正确,点击100分题也永远匹配不到对应分支。 - 答案校验写死:提交答案时固定用
q1.getAnswer()做校验,不管玩家选哪道题,都只会和第一题的答案做比对,后续扩展多题时逻辑完全不成立。
修复后代码
首先把需要跨事件保留的状态变量定义为类成员变量,再修正分支笔误和答案校验逻辑:
public class GUIWindow implements ActionListener{ Players p1 = new Players(); private int p1Score = 0; private int p2Score = 0; // 移到类成员位置,存储当前选中题目的分值,跨事件调用不会重置 private int currentPoints = 0; // 存储当前选中的题目对象,替代写死的q1校验 private Question currentQuestion = null; public void actionPerformed(ActionEvent e){ // 移除方法内的局部points定义,避免每次调用重置 for(int i = 0; i < 5; i++){ if(e.getSource() == boardButtonsRow1[i]){ currentPoints = 500; // 替换为你实际存储每行题目的数组变量 // currentQuestion = row1Questions[i]; boardButtonsRow1[i].setEnabled(false); }else if(e.getSource() == boardButtonsRow2[i]){ currentPoints = 400; // currentQuestion = row2Questions[i]; boardButtonsRow2[i].setEnabled(false); }else if(e.getSource() == boardButtonsRow3[i]){ currentPoints = 300; // currentQuestion = row3Questions[i]; boardButtonsRow3[i].setEnabled(false); }else if(e.getSource() == boardButtonsRow4[i]){ currentPoints = 200; // currentQuestion = row4Questions[i]; boardButtonsRow4[i].setEnabled(false); }else if(e.getSource() == boardButtonsRow5[i]){ // 修正笔误,改为第5行按钮数组 currentPoints = 100; // currentQuestion = row5Questions[i]; boardButtonsRow5[i].setEnabled(false); } } if(e.getSource() == submitBtn){ String answer = answerEntry.getText(); // 校验当前选中题目对应的答案,替代写死的q1 if(currentQuestion != null && answer.equalsIgnoreCase(currentQuestion.getAnswer())){ System.out.println("Correct"); System.out.println(currentPoints); p1Score += currentPoints; p1.setPoints(p1Score); showP1ScoreLbl.setText(""+p1.getScore()); // 答完重置状态,避免重复提交 currentPoints = 0; currentQuestion = null; } } } }
额外优化建议
- 点击题目按钮后直接将按钮置灰,避免同一道题被重复选中
- 答题完成后清空输入框、重置当前选题状态,适配下一轮答题流程
- 可以把分值直接存在按钮的客户端属性里,不用硬编码每行对应的分值,减少分支判断代码量
内容的提问来源于stack exchange,提问作者ChefCurry
相关产品推荐
相关产品推荐

