You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于数组计算总分并在表格中展示:保龄球游戏计分Java类循环问题排查

问题分析与修复方案

我帮你梳理下代码里的核心问题,主要集中在总分计算和分数展示的循环逻辑上:

1. 总分计算逻辑错误(calculateTotal方法)

你当前的代码把所有玩家的所有局分数都累加到了totals[0],这会导致只有第一个玩家的总分是所有玩家的分数总和,其他玩家的总分始终是初始值0。正确逻辑应该是每个玩家单独累加自己的每局分数。

2. 分数展示格式错误(showScores方法)

  • 打印每局分数时错误拼接了totals[0],导致每列分数后面都跟着第一个玩家的总分,格式完全混乱;
  • 没有正确打印每个玩家对应的总分,缺少了总分列的输出。

3. 实时更新总分的时机问题

在addScores方法中,每次添加完分数后直接调用showScores,但没有先更新总分,导致展示的总分始终是旧值(初始0)。


修复后的完整代码

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package cis.entity;

import cis.util.CisUtility;
import java.util.Scanner;

/**
 *
 * @author mm
 */
public class Game {
    private static final int NUMBER_OF_ENDS = 10;
    private static final int NUMBER_OF_PLAYERS = 4;
    private static final String[] player = new String[NUMBER_OF_PLAYERS];
    private static final int[][] scores = new int[NUMBER_OF_PLAYERS][NUMBER_OF_ENDS];
    int[] totals = new int[NUMBER_OF_PLAYERS];

    public void addPlayer() {
        for (int counter = 0; counter < player.length; counter++) {
            player[counter] = CisUtility.getInputString("Enter name of player #" + (counter + 1));
        }
    }

    public void addScores() {
        for (int endIndex = 0; endIndex < NUMBER_OF_ENDS; endIndex++) {
            for (int playerIndex = 0; playerIndex < player.length; playerIndex++) {
                scores[playerIndex][endIndex] = CisUtility.getInputInt("Enter score number " + (endIndex + 1) + " score for " + player[playerIndex] + "---->");
                calculateTotal(); // 添加分数后先更新总分
                showScores();
            }
        }
    }

    public void showScores() {
        // 清屏(可选,让展示更清晰)
        System.out.print("\033[H\033[2J");
        System.out.flush();
        
        System.out.println("Player \tEnd #1 \tEnd #2 \tEnd #3 \tEnd #4 \tEnd #5\tEnd #6 \tEnd #7 \tEnd #8 \tEnd #9 \tEnd #10 \tTotals");
        for (int playerIndex = 0; playerIndex < NUMBER_OF_PLAYERS; playerIndex++) {
            System.out.print(player[playerIndex] + "\t");
            // 打印每局的分数
            for (int endIndex = 0; endIndex < NUMBER_OF_ENDS; endIndex++) {
                System.out.print(scores[playerIndex][endIndex] + "\t");
            }
            // 打印当前玩家的总分
            System.out.println(totals[playerIndex]);
        }
        System.out.println("\n");
    }

    public void calculateTotal() {
        // 先重置所有玩家的总分,避免重复累加
        for (int i = 0; i < totals.length; i++) {
            totals[i] = 0;
        }
        // 每个玩家累加自己的每局分数
        for (int playerIndex = 0; playerIndex < NUMBER_OF_PLAYERS; playerIndex++) {
            for (int endIndex = 0; endIndex < NUMBER_OF_ENDS; endIndex++) {
                totals[playerIndex] += scores[playerIndex][endIndex];
            }
        }
    }
}

关键修复点说明

  • calculateTotal方法:先重置所有玩家的总分(避免多次调用时重复累加),然后为每个玩家单独累加自己的每局分数到totals[playerIndex];
  • showScores方法:移除了每局分数后的错误totals[0],改为在每一行末尾打印当前玩家的totals[playerIndex],同时添加了清屏逻辑让展示更整洁;
  • addScores方法:在调用showScores前先调用calculateTotal,确保每次展示的都是最新的总分。

内容的提问来源于stack exchange,提问作者cattedad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 12:22:44