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

高尔夫Player类recordPlayer方法实现求助:跨类调用与规则适配

高尔夫项目Player类recordPlayer方法实现方案

核心规则回顾

  • 仅当击球位置为"Fairway"或"Green"时,击球视为On Target
  • 满足以下条件时击球算作Full Drive:
    1. 球洞长度≥400码(通过Course.isFullDriveHole(hole)判断)
    2. 击球位置为On Target
  • 仅Full Drive参与最长击球距离的统计

原代码问题修正与方法实现

第一步:修复Player构造函数语法错误

原Player类构造函数的初始化代码写在构造函数外部,属于语法错误,需将成员变量初始化移至构造函数内部:

public class Player {
    private String name;
    private ScoreCard card;
    private int longestDrive;
    private int fullDriveYards;
    private int fullDrives;
    private int drivesOnTarget;

    public Player(String playerName) {
        if (playerName == null) {
            this.name = "Unknown";
        } else {
            this.name = playerName;
        }
        // 将初始化代码移至构造函数内
        this.card = new ScoreCard();
        this.longestDrive = 0;
        this.fullDriveYards = 0;
        this.fullDrives = 0;
        this.drivesOnTarget = 0;
    }

    // 其余方法...
}

第二步:实现recordPlayer方法

修正方法内的错误逻辑(如重复创建ScoreCard、无效参数调用),并按照规则更新统计变量:

public void recordPlayer(int hole, int strokes, int driveYds, String location) {
    // 验证球洞有效性:至少保证球洞为正整数,可根据球场总洞数补充上限(比如18)
    if (hole < 1 || hole > 18) {
        System.out.println("无效的球洞编号");
        return;
    }

    // 调用ScoreCard记录当前洞的成绩
    this.card.recordScore(hole, strokes);

    // 判断是否为On Target
    boolean isOnTarget = "Fairway".equals(location) || "Green".equals(location);

    // 更新On Target统计
    if (isOnTarget) {
        this.drivesOnTarget++;
    }

    // 判断是否为Full Drive
    boolean isFullDrive = Course.isFullDriveHole(hole) && isOnTarget;
    if (isFullDrive) {
        this.fullDrives++;
        this.fullDriveYards += driveYds;
        // 更新最长Full Drive距离
        if (driveYds > this.longestDrive) {
            this.longestDrive = driveYds;
        }
    }
}

第三步:完善其他辅助方法

原代码中computeFullAvg和computeAccuracy返回固定值,需实现实际计算逻辑:

// 计算Full Drive平均距离
public double computeFullAvg() {
    return this.fullDrives == 0 ? 0.0 : (double) this.fullDriveYards / this.fullDrives;
}

// 计算击球准确率(On Target次数 / 总击球次数)
public double computeAccuracy() {
    int totalDrives = this.card.getHolesPlayed();
    return totalDrives == 0 ? 0.0 : (double) this.drivesOnTarget / totalDrives * 100;
}

// 修正命名规范:原fullDriveYds改为getFullDriveYards
public int getFullDriveYards() {
    return this.fullDriveYards;
}

// 完善toString方法,补充返回值
public String toString() {
    String playerReport = this.card.toString();

    // 添加击球统计信息
    playerReport += "\nLongest Full Drive: " + this.getLongestDrive() + "码";
    playerReport += "\nAverage Full Drive: " + String.format("%.1f", this.computeFullAvg()) + "码";
    playerReport += "\nDriving Accuracy: " + String.format("%.1f%%", this.computeAccuracy());
    return playerReport;
}

关键逻辑说明

  1. 避免覆盖成绩:不再每次调用recordPlayer时新建ScoreCard,而是使用成员变量this.card保留历史成绩
  2. 有效性验证:先判断球洞编号是否合法,避免无效数据进入统计
  3. 规则落地:严格按照题目给定的On Target、Full Drive规则逐一判断,更新对应统计变量
  4. 边界处理:计算平均值和准确率时,处理分母为0的情况,避免除零错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 08:45:06