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

基于链表的棒球模拟游戏:innings循环与重玩逻辑问题修复

棒球模拟游戏链表实现问题修复

核心问题修复点

  • 重玩条件控制:移除main方法内的即时重玩询问,改为完成至少4局且总得分≥1后,再触发重玩提示
  • 多局循环实现:在startGame中添加外层循环控制局数,每局重置出局数、单局安打数,保留全局总得分
  • 第二局异常修复:每局开始重置单局变量,击球手从全局当前位置继续,而非每次回到链表头部
  • 避免无限循环:重玩逻辑与局循环分离,全局初始化Scanner避免输入异常,提前将链表设为循环结构

修改后的完整代码

import java.util.Random;
import java.util.Scanner;

public class Baseball {
    private Players head, tail;
    private Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        Baseball game = new Baseball();
        game.addPlayer(1, "Ted Williams", 0.344, 0.0687);
        game.addPlayer(2, "Rogers Hornsby", 0.359, 0.0375);
        game.addPlayer(3, "Mark McGwire", 0.265, 0.095);
        game.addPlayer(4, "Babe Ruth", 0.340, 0.085);
        // 初始化循环链表,避免重复设置
        game.tail.next = game.head;

        boolean keepPlaying = true;
        while (keepPlaying) {
            int totalScore = game.startGame();
            // 满足条件才询问重玩
            if (totalScore >= 1) {
                System.out.print("\nPlay again?: Y/N ");
                String input = game.scanner.nextLine();
                if (input.equalsIgnoreCase("N")) {
                    keepPlaying = false;
                }
            }
        }
        game.scanner.close();
    }

    private int startGame() {
        int totalScore = 0;
        int inning = 0;
        Players currentBatter = head;
        Random random = new Random();

        // 至少进行4局
        while (inning < 4) {
            inning++;
            int outs = 0;
            int hits = 0;
            int htct = 0;
            int ct = 0;

            System.out.printf("\n ------------- Inning: %d - Game Score: %d Runs", inning, totalScore);

            while (outs < 3) {
                ct++;
                double rr = random.nextDouble();

                if (currentBatter.hrAvg > rr) {
                    System.out.printf("\n\nBatter %d - %s ->  BA: %.3f Random Number: %.2f HOMERUN", ct, currentBatter.name, currentBatter.bAvg, rr);
                    totalScore += 1;
                    hits++;
                } else if (currentBatter.bAvg > rr) {
                    System.out.printf("\n\nBatter %d - %s ->  BA: %.3f Random Number: %.2f HIT", ct, currentBatter.name, currentBatter.bAvg, rr);
                    htct++;
                    // 每3次安打得1分,重置计数
                    if (htct >= 3) {
                        totalScore += 1;
                        hits++;
                        htct = 0;
                    }
                } else {
                    System.out.printf("\n\nBatter %d - %s ->  BA: %.3f Random Number: %.2f Miss", ct, currentBatter.name, currentBatter.bAvg, rr);
                    outs++;
                }

                currentBatter = currentBatter.next;

                if (outs == 3) {
                    System.out.printf("\n--------------- Inning Totals: %d Runs %d Hits", hits, hits + htct);
                }
            }
        }
        return totalScore;
    }

    private class Players {
        Integer bOrder;
        String name;
        Double bAvg;
        Double hrAvg;
        Players previous;
        Players next;

        public Players(int bOrder, String name, Double bAvg, Double hrAvg) {
            this.bOrder = bOrder;
            this.name = name;
            this.bAvg = bAvg;
            this.hrAvg = hrAvg;
            this.next = null;
            this.previous = null;
        }
    }

    public void addPlayer(int bOrder, String name, Double bAvg, Double hrAvg) {
        Players player = new Players(bOrder, name, bAvg, hrAvg);
        if (head == null) {
            head = player;
            tail = player;
            head.previous = null;
        } else {
            tail.next = player;
            player.previous = tail;
            tail = player;
        }
    }
}

关键修复说明

  1. 循环链表初始化:仅在添加完所有球员后设置一次tail.next = head,避免重复操作导致的逻辑混乱
  2. 击球手连续顺序:每局结束后保留当前击球手位置,下一局从该位置的下一位继续
  3. 得分逻辑优化:明确本垒打直接得分,安打累计3次得分并重置计数
  4. 重玩触发条件:只有完成4局且总得分≥1时,才会显示重玩询问

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 09:05:21