尝试引用另一种方法时出现一些错误

时间:2013-11-08 22:57:13

标签: java

所以我正在制作一个足球比赛计划,最终我希望它看起来像这样,http://i.imgur.com/qFaUrKv.png

我在handleTeamScoring()方法中引用我的getChoice()方法时遇到问题。我的选择,得分,footballTeam1,footballTeam2和FootballTeam在我的.getscore部分得到了红线。

这是为什么?我如何参考其他方法? 任何帮助表示赞赏,谢谢。 这是我的代码。 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;






}
}

这是我的另一堂课。

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

}
 }

2 个答案:

答案 0 :(得分:0)

要使用其中一种方法,您需要创建类FotballTeam对象

FootballTeam team = new FootballTeam(fotballTeam1, 0);创建一个名为"Sweden"且得分为3的团队,并将其存储在变量中。

然后您可以使用此变量并调用team.getScore();

这将向您展示如何创建FootballTeam个对象并使用它们的基础知识:

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());

最好将此变量存储在您的类中(不在方法中),以便您可以随意访问它。

FotballTeam.getScore()尝试使用该方法,就好像它是静态的一样,但它不是。 Read here for the difference between static and non-static methods

您也可以阅读Sun's Java tutorial about classes and objects

答案 1 :(得分:0)

尝试声明您的本地变量 - 当方法启动时生成并在方法结束时死亡 - 如全局,这意味着您可以使用任何非静态方法。通过全局变量,我的意思是像Scanner类的“键盘”对象。删除方法的静态语句,创建一个名为“choice”的字符串,它不是静态的。因此,您将看到可以在方法中使用这些字段。