1

So I'm making a football game program that in the end I want it to look like this, https://i.stack.imgur.com/q9yIP.png

I'm having trouble referring to my getChoice() method within my handleTeamScoring() method. I'm getting redlines on choice, score, footballTeam1, footballTeam2, and FootballTeam in my .getscore part of that method.

Why is this? How do I refer to the other method? Any help is appreciated, thanks. Here's my code. https://gist.github.com/anonymous/beec75b6361ff1fcbc28

 package footballgame;

 import java.util.Scanner;

 public class FootballGame {

static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {
    String choice;
    Scanner keyboard = new Scanner(System.in);
    String footballTeam1;
    String footballTeam2;

    System.out.print("Enter a name for a team:");
    footballTeam1 = keyboard.nextLine();
    System.out.print("Enter a name for another team:");
    footballTeam2 = keyboard.nextLine();

    System.out.println("Game Score:");
    System.out.println(footballTeam1 + ":0");
    System.out.println(footballTeam2 + ":0");

    choice = getMenuChoice(footballTeam1, footballTeam2);
}

public static String getMenuChoice(String footballTeam1, String footballTeam2) {
    String choice = "";
    String input;


    do {
        System.out.println("Select an option:");
        System.out.println("A:" + footballTeam1 + " scored");
        System.out.println("B:" + footballTeam2 + " scored");
        System.out.println("C: game ended.");
        System.out.println("?:");
        input = keyboard.nextLine();
        if (input.equalsIgnoreCase("A")) {
            choice = (footballTeam1);
        } else if (input.equalsIgnoreCase("B")) {
            choice = (footballTeam2);
        } else if (input.equalsIgnoreCase("C")) {
            choice = ("Game over!");
        }



    } while (!input.equals("A") && !input.equals("B") && !input.equals("C"));
    return choice;

}
 public static int handleTeamScore(int footballTeam) {

   do {
    System.out.println("How many points were scored by " + choice);
    int Keyboard = keyboard.nextInt();

  if (points == 1) {
      return footballTeam; }
  if (points == 2) {
      return footballTeam; }
  if (points == 3) {
      return footballTeam; }
  if (points == 6) {
      return footballTeam; }
   } while ( footballTeam != 1 || footballTeam != 2 || footballTeam != 3 || footballTeam != 6); {
}
    System.out.println("Game Score: ");       
    System.out.println(footballTeam1 + ": " + FootballTeam.getScore());
    System.out.println(footballTeam2 + ": " + FootballTeam.getScore());
    return footballTeam;






}
}

And here is my other class.

public class FootballTeam {

private String name;
private int score;
public static int TOUCHDOWN = 6;
public static int FIELD_GOAL = 3;
public static int SAFETY = 2;
public static int TWO_POINT_CONVERSION = 2;
public static int EXTRA_POINT = 1;

public FootballTeam(String name, int score) {
    this.name = name;
    this.score = score;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getScore() {
    return score;
}

public void setScore(int score) {
    this.score = score;
}

public boolean addScore(int points) {
    if (points == TOUCHDOWN || points == FIELD_GOAL || points == SAFETY || points == TWO_POINT_CONVERSION || points == EXTRA_POINT) {
        score = points + score;
        return true;
    } else {
        return false;
    }

}
 }
Tonno22
  • 167
  • 2
  • 10
  • Please edit your question and add the code to it. We try to not depend on 3rd party sources for that, since links have a tendency to become broken after a while. – Simon Forsberg Nov 08 '13 at 22:59
  • Hover your cursor over those read lines and they would tell you your error with a possible solution. i.e. if you are using any IDE like eclipse. And BTW I can't find your `getChoice()` and `handleTeamScoring()` methods in link you have. – Prateek Nov 08 '13 at 23:01
  • @Prateek He probably means `getMenuChoice` and `handleTeamScore` – Simon Forsberg Nov 08 '13 at 23:07
  • I added the code to my original post, thanks – Tonno22 Nov 08 '13 at 23:07
  • Yeah that's what I meant Simon – Tonno22 Nov 08 '13 at 23:07

2 Answers2

0

In order to use one of those methods, you will need to create an object of the class FotballTeam

FootballTeam team = new FootballTeam(fotballTeam1, 0); creates a team with name "Sweden" and score 3 and stores that in a variable.

Then you can use this variable and call team.getScore();

This will show you the basics of how to create FootballTeam objects and use them:

System.out.print("Enter a name for a team:");
footballTeam1 = keyboard.nextLine();
System.out.print("Enter a name for another team:");
footballTeam2 = keyboard.nextLine();

FootballTeam team1 = new FootballTeam(footballTeam1, 0);
FootballTeam team2 = new FootballTeam(footballTeam2, 0);
System.out.println("Game Score:");
System.out.println(team1.getName() + ":" + team1.getScore());
System.out.println(team2.getName() + ":" + team2.getScore());

Preferably you should store this variable in your class (not within a method) so that you can access it anywhere you want.

FotballTeam.getScore() tries to use the method as if it were static, which it is not. Read here for the difference between static and non-static methods

You also might want to read Sun's Java tutorial about classes and objects

Community
  • 1
  • 1
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • But I already made the user enter the names for the football teams above, so what would I have in my parameter? – Tonno22 Nov 08 '13 at 23:10
  • @user2934545 Answer updated. You will probably want to learn about `variable scope` later on, as you will need to access your `FootballTeam`-objects. – Simon Forsberg Nov 08 '13 at 23:15
0

Just try to declare your local variables -that born when method starts and die when method ends- as global, which means you can use them any non-static method. By global variable, i mean like your "keyboard" object of Scanner class. Delete static statements of methods, create a String named "choice" which is not static. So you will see that you can use those fields in your methods.