无法解析为java程序中的变量

时间:2013-09-15 19:57:24

标签: java

当我编译这段代码时,我得到的唯一错误是“胜利者无法解析为变量”或“令牌上的语法错误”获胜者“,删除此令牌”我理解错误与我的if-else语句有关,但我不知道我做错了什么,或者我只是以错误的方式做错。是否有更好的方法可以在两支随机统计数据的队伍中取胜?

public class Team {

    /**
     * The name of the team.
     */
    public String name;

    /**
     * The location of the team.
     */
    public String location;

    /**
     * The offensive strength of the team.
     */
    public double offense;

    /**
     * The defensive strength of the team.
     */
    public double defense;

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

    /**
     * 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) {
        double home = (this.offense + this.defense + 0.2) * this.luck();
        double away = (visitor.offense + visitor.defense) * visitor.luck();
        if (home > away) {

        }
        else {
          away = winner;
        }
        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>
     * (Note that name and location should be separate lines for each team)
     * 
     * @param args can be ignored.
     */
    public static void main(String[] args) {
        // Be sure to follow the same printing command to ask user
        // for name and location of two teams (e.g, home and away)
        System.out.println("Enter name and location for home team (on separate lines)");
        // Get input from user for home team.
        // Create a home team object with the given data.
        Scanner in = new Scanner(System.in);
        team = in.nextLine();
        location = in.nextLine();
        Team home = new Team(name, location, offense, defense);
        System.out.println("Enter name and location for away team (on separate lines)");
        // Get input from user for away team.
        // Create an away team object with the given data.
        team = in.nextLine();
        location = in.nextLine();
        Team away = new Team(name, location, offense, defense);
        // Print out home team information.
        System.out.println("Home team is: " + home.name + " from " + home.location + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
        // Print out away team information.

        System.out.println("Away team is: " + away.name + " from " + away.location + " rated <away_team_offense> (offense) + <away_team_defense> (defense)");

        // Call the home team's play() method with the away team as visitor parameter.
        Team winner = home.play(away);
        // Print out the winner.
        System.out.println("Winner is:" winner.name + "from " + winner.location + " rated <winner_team_offense> (offense) + <winner_team_defense> (defense)");
        // (be sure to adhere to the format described below)
    }
}

3 个答案:

答案 0 :(得分:4)

您没有声明winner。像这样改变play()方法

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

或者如果你想只有一个return(这是一个好主意) -

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

    // depending on the score, return winning team. 
    return (home > away)? this : visitor;
}

最后,您可能想要考虑如果分数相同,即home == away会发生什么。如果分数相等,则当前代码将使visitor获胜。

答案 1 :(得分:2)

正如@Sotirios Delimanolis指出的那样,问题是winner是静态方法main中的局部变量,而不是类成员变量(又称字段),所以它在它之外是不可见的。

答案 2 :(得分:0)

获胜者直到主要时才被初始化。我认为你的方法play()是错误的。它应该写成:

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

或其他相似之处。 另请注意,您最初是为团队Winner(一个在主要方法之外不存在的团队)设置(双倍)