引用和调用另一个类中的方法时出现的问题

Problems with referencing and calling methods in another class

提问人:Alyda96 提问时间:7/6/2023 最后编辑:Alyda96 更新时间:7/7/2023 访问量:33

问:

我不确定如何在高尔夫球手类中设置方法。这个类调用另一个名为 ScoreCard 的类的其他方法,所以我也会在这里包括该类。 玩家等级:

    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;
    }

    // This is the method I need help on
    public void recordPlayer(int hole, int strokes, int driveYds, String    location)
    {
    // make sure the hole number is  valid
    this.card  = new ScoreCard();
    card.getHolesPlayed();
    if (true)
    {
        // replace condition below with a test for valid hole
        // I will list the conditions in a second after I am done showing     the classes
        // record player's score on this hole
        card.getTotalStrokes();
        // use this.card and call on a ScoreCard method
        this.card.recordScore(0, 0);
        // define the conditions under which values of each of the int-type
        // variables are adjusted and the new values they will have

        }
    public String getName()
    {
        return this.name;
    }
    
    public int getLongestDrive()
    {
        return this.longestDrive;
    }

    public int getFullDrives()
    {
        return this.fullDrives;
    }

    public int fullDriveYds()
    {
        return this.fullDriveYards;
    }

    public double computeFullAvg()
    {
        return 0.0;
    }

    public int getDrivesOnTarget()
    {
        return this.drivesOnTarget;
    }

    public double computeAccuracy()
    {
        return 0.0;
    }

    public String toString()
    {
        String playerReport = this.card.toString();

        // add their driving statitics
        playerReport = playerReport + "\nLongest Full Drive: " + 
            this.getLongestDrive();
        playerReport = playerReport + "\nAverage Full Drive: " + 
            this.computeFullAvg();
        playerReport = playerReport + "\nDriving Accuracy: " + 
            this.computeAccuracy();
    }
}    

下面是 ScoreCard 类:

    public class ScoreCard
    {
        private int totalStrokes;
        private int versusPar;
        private LinkedList<Integer> playerScoreCard;

    public ScoreCard()
    {
        // initialise instance variables
        totalStrokes = 0;
        versusPar = 0;
        playerScoreCard = new LinkedList<Integer>();
    }

    public void recordScore(int inHole, int inStrokes)
    {
        // put your code here
        if ((inHole >= 1) && (inHole <= playerScoreCard.size() + 1)) {
            playerScoreCard.add(inHole - 1, inStrokes);
        }
        totalStrokes += inStrokes;
        versusPar += (inStrokes - Course.getHole(inHole).getPar());
    }

    public int getHolesPlayed()
    {
        return playerScoreCard.size();
    }

     public int getTotalStrokes()
    {
        return totalStrokes;
    }

    public int getVersusPar()
    {
        return versusPar;
    }

public String toString()
    {
        return "Holes Played: " + getHolesPlayed() + "\n" +
                "Total Strokes: " + getTotalStrokes() + "\n" +
                "Versus Par: " + getVersusPar();
    }
}

现在,这里是 Player 类 recordPlay 方法的条件: recordPlay 方法调用相关 ScoreCard 实例的方法,以记录球员在球洞上的得分。IT 还使用以下规则调整其他实例变量的值:

  1. 仅当记录的位置为“球道”或“果岭”时,才认为驱动器在目标上。
  2. 如果满足以下条件,则驱动器计为完整驱动器:a) 它被记录在足够长度的孔上(任何长度为 400 码或更大,并使用 isFullDriveHole) b) 驱动器位置在目标上。
  3. 只有完整的驱动器才被认为是最长的驱动器

除了 recordPlay 方法之外,我几乎完成了所有事情,这真的给我带来了一些麻烦

Java 对象 方法 引用 实例变量

评论

0赞 tgdavies 7/6/2023
“真的给我带来了一些麻烦”到底是什么麻烦?
0赞 Alyda96 7/6/2023
我不确定如何定义条件

答: 暂无答案