2

When I compile this code, the only errors I get say "winner cannot be resolved to variable" or "Syntax error on token "winner", delete this token" I understand the error has to do with my if-else statement, but I don't know what I doing wrong, or if I'm just doing it the wrong way. Is there a better way to go about making a winner between two teams with random stats?

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)
    }
}
4
  • 3
    Consider the scope of the winner variable in the main() method. Commented Sep 15, 2013 at 19:58
  • @SotiriosDelimanolis put your comment as an answer Commented Sep 15, 2013 at 20:03
  • @LexLythius You guys got this. Commented Sep 15, 2013 at 20:03
  • Wow, thanks for such a fast answer guys! That solved my problem for the first error, but I'm still getting the "Syntax error on token "winner", delete this token" error, one option is obviously to delete it, but I need it to print who the winner is. How do I fix this? Commented Sep 15, 2013 at 20:17

3 Answers 3

4

You don't have winner declared. Change play() method like this

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

OR if you want to have just one return(which is a good idea) -

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

Lastly, you might want to consider what happens if the scores are same i.e. home == away. The current code will make the visitor win if the scores are equal.

Sign up to request clarification or add additional context in comments.

1 Comment

“One return only” is a divisive rule. Some people think it is important, and others reject it utterly. In this case, it doesn't make much difference, but in others it is pretty profound.
2

As @Sotirios Delimanolis pointed out, the issue is that winner is a local variable within static method main, not a class member variable (aka a field), so it's not visible outside of it.

Comments

0

Winner isn't being initialized until main. I think your method play() is wrong. It should be written as:

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

or something to that effect. Also note that you are originally setting away (a double) to team Winner (a team that doesn't exist outside of the main method)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.