0

我正在尝试自己学习 Java,但我正在做一个问题。我正在创建一个程序,该程序从用户那里获取输入以制作两个 Team 对象,然后计算获胜者。每个团队都有一个名称和位置,表示为 String 对象,以及表示为双值的进攻和防御能力,使用luck() 方法在 0 和 1 之间随机初始化。

import java.util.Scanner;

public class Team {
    public String name;
    public String location;
    public double offense;
    public double defense;
    public String awayName;
    public String awayLocation;
    public double awayOffense;
    public double awayDefense;


    /**
     * Create a team with specified name and location, and with offense and defense    capabilities
     * randomly initialized using the luck() method.
     */
    public Team(String name, String location) {
        this.name = name;
        this.location = location;
    }     

    /**
     * The luck() method returns a random value between 0 and 1, using Math.random().
     * 
     * @returns a real value in range [0,1]
     */
    public double luck() {
        return Math.random();
    }

    /**
     * Run a competition against the specified visiting team
     * 
     * @param other the team to play against
     * @returns the winner team
     */
    Team play(Team visitor) {
      home = (this.offense + this.defense + 0.2) * this.luck();
      away = (visitor.offense + visitor.defense) * visitor.luck();
      return winner;  
    }

    /**
     * Run a competition between two teams specified on standard input.
     * Print statistics of the winner.
     * <p>
     * Each team is read in the following format :
     * <pre>
     * &lt;name&gt; 
     * &lt;location&gt;
     * </pre>
     */
    public static void main(String[] args) {
        System.out.println("Enter name and location for home team (on separate lines)");
        Scanner tn = new Scanner(System.in);
        Team team = new Team("Name","Location");
        team.name = tn.nextLine(); 

        Scanner tl = new Scanner(System.in);
        team.location = tl.nextLine();

        Team home = new Team(team.name, team.location);


        System.out.println("Enter name and location for away team (on separate lines)"); 
        Scanner atn = new Scanner(System.in);
        team.awayName = atn.nextLine(); 

        Scanner atl = new Scanner(System.in);
        team.awayLocation = atl.nextLine();

        Team away = new Team(team.awayName, team.awayLocation);


        System.out.println("Home team is: " + team.name+ " from " + team.location + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
        System.out.println("Away team is: " + team.awayName+ " from " + team.awayLocation + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");


        // Create a home and an away team object with the given data.
        // Call the home team's play() method with the away team as visitor parameter.
        // Print out the winner.

评论部分告诉我我需要做什么,尽管我不确定从这里去哪里。我被卡住了,认为到目前为止我所做的甚至可能都不正确,有人可以推动我朝着正确的方向前进吗?请帮忙!谢谢!

4

2 回答 2

2

你自己已经走得很好了。

一旦你输入了进攻和防守的球队统计数据(为什么不将它们添加到构造函数参数中?),你可以像这样对它们进行对抗:

Team winner = home.play(away);

你的播放方法应该是这样的:

Team play(Team visitor) {
    double home = (this.offense + this.defense + 0.2) * this.luck();
    double away = (visitor.offense + visitor.defense) * visitor.luck();
    Team winner = (home > away ? this : visitor);
    return winner;
}

您询问了如何随机设置评分 - 如果您希望它只发生一次,构造函数将是执行此操作的理想场所:

public Team(String name, String location) {
    this.name = name;
    this.location = location;
    awayOffense = luck();
    awayDefense= luck();
}
于 2013-09-11T12:19:32.610 回答
0

我可能是错的,但你没有用一个值来初始化进攻和防守。这样可能会更好:

public Team(String name, String location, double offence, double defence) {
    this.name = name;
    this.location = location;
    this.offence = offence;
    this.defence = defence;
}     
于 2013-09-11T12:18:17.277 回答