Java中静态变量每次调用函数时重复初始化问题求助
嘿,我来帮你搞定这个分数累加和结果记录的问题~首先咱们先揪出核心问题:你现在的UserScore和computerScore每次重启游戏就重置,大概率是因为你在游戏启动的逻辑里又把它们重新赋值为0了!虽然你用了ArrayList记录结果,但分数的累加逻辑没跟上,而且单纯存Integer的记录方式也有点局限。
先解决分数累加的核心问题
你已经把UserScore和computerScore声明为类的静态变量了,这步是对的,但要删掉每次游戏开始时把它们设为0的代码——静态变量只会在类加载时初始化一次(也就是static int UserScore = 0;这一行),只要不在游戏循环里重复赋值0,它们就能一直累加下去。
比如原来如果有这样的代码:
// 错误:每次重启游戏都重置分数 public void startGame() { UserScore = 0; computerScore = 0; // ...游戏逻辑 }
把这两行初始化代码删掉就好,只保留类层面的静态初始化。
结果记录的更优方案(替代单纯的ArrayList)
单纯用ArrayList<Integer>只能存单一数值,没法区分是用户还是电脑的得分,也没法记录每局的胜负情况,这里给你几个更实用的方案:
1. 自定义类封装每局结果(最推荐)
创建一个GameResult类,把每局的关键信息都封装进去,这样记录的信息更完整,后续查看也更清晰:
class GameResult { private int userRoundScore; // 本局用户得分 private int computerRoundScore; // 本局电脑得分 private String roundResult; // 本局结果,比如"用户获胜"、"平局" // 构造方法 public GameResult(int userRoundScore, int computerRoundScore, String roundResult) { this.userRoundScore = userRoundScore; this.computerRoundScore = computerRoundScore; this.roundResult = roundResult; } // 可选:添加getter方法方便读取数据 public int getUserRoundScore() { return userRoundScore; } public int getComputerRoundScore() { return computerRoundScore; } public String getRoundResult() { return roundResult; } }
然后把原来的ArrayList<Integer>换成static List<GameResult> gameHistory = new ArrayList<>();,每局结束后就把结果存进去:
// 每局结束后记录 gameHistory.add(new GameResult(userRoundScore, computerRoundScore, "用户获胜"));
2. 用Map存储分维度的得分记录
如果不想自定义类,也可以用Map来分别存储用户和电脑的每局得分,比如:
static Map<String, List<Integer>> scoreHistory = new HashMap<>(); // 初始化时创建两个列表 scoreHistory.put("userScores", new ArrayList<>()); scoreHistory.put("computerScores", new ArrayList<>());
每局结束后往对应的列表里加分数:
scoreHistory.get("userScores").add(userRoundScore); scoreHistory.get("computerScores").add(computerRoundScore);
这种方式也能区分开双方的每局得分,后续要算累计分数的话,直接对列表求和就行。
3. 持久化记录(可选)
如果希望关闭程序后还能保留历史分数,可以把记录写入本地文件(比如txt或csv),用BufferedWriter实现,下次启动程序时再读取文件恢复分数和历史记录。不过如果只是单次运行期间的记录,用内存中的集合就足够了。
简单的代码示例
调整后的游戏循环大概是这样的:
import java.util.*; class OddsAndEvens { static int UserScore = 0; static int computerScore = 0; static List<GameResult> gameHistory = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String playAgain; do { // 模拟游戏逻辑,这里替换成你实际的游戏代码 int userRoundScore = 1; int computerRoundScore = 0; String roundResult = "用户获胜"; // 更新累计分数 UserScore += userRoundScore; computerScore += computerRoundScore; // 记录本局结果 gameHistory.add(new GameResult(userRoundScore, computerRoundScore, roundResult)); // 询问是否继续 System.out.print("输入'y'重新开始游戏:"); playAgain = scanner.nextLine(); } while (playAgain.equalsIgnoreCase("y")); // 打印最终结果 System.out.println("\n游戏结束!"); System.out.println("累计用户得分:" + UserScore); System.out.println("累计电脑得分:" + computerScore); System.out.println("\n每局详细结果:"); for (int i = 0; i < gameHistory.size(); i++) { GameResult result = gameHistory.get(i); System.out.printf("第%d局:用户得分%d,电脑得分%d,结果:%s%n", i+1, result.getUserRoundScore(), result.getComputerRoundScore(), result.getRoundResult()); } } } // 自定义的GameResult类 class GameResult { private int userRoundScore; private int computerRoundScore; private String roundResult; public GameResult(int userRoundScore, int computerRoundScore, String roundResult) { this.userRoundScore = userRoundScore; this.computerRoundScore = computerRoundScore; this.roundResult = roundResult; } public int getUserRoundScore() { return userRoundScore; } public int getComputerRoundScore() { return computerRoundScore; } public String getRoundResult() { return roundResult; } }
这样调整后,每次输入'y'重启游戏时,累计分数会一直累加,而且每局的详细结果也能完整记录下来啦~
内容的提问来源于stack exchange,提问作者Aditya Ashtikar

