当我调用我的getChoice()方法时,我在这里收到错误,我不知道为什么

时间:2013-11-09 04:06:48

标签: java

我收到一个错误,我调用了我的getChoice()方法,它的说法变量footballTeam2已在main方法中定义,为什么会这样?我也正确地调用我的其他方法吗? 在此先感谢你们,真的可以使用一些帮助。

import java.util.Scanner;

public class FootballGame { 
    static Scanner keyboard = new Scanner(System.in);
    static int arewedone = 0;
    static String choice;
    static FootballTeam team;

public static void main(String[] args) {

    FootballTeam footballTeam1;
    FootballTeam footballTeam2;

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

    do{
        System.out.println("Game Score:");
        System.out.println(footballTeam1.getName() + ":" + footballTeam1.getScore());
        System.out.println(footballTeam2.getName() + ":" + footballTeam2.getScore());

        choice = getMenuChoice(FootballTeam footballTeam1, FootballTeam footballTeam2);
        handleTeamScore(team);

    }while(arewedone == 0);
}

public static String getMenuChoice(FootballTeam footballTeam1, FootballTeam footballTeam2) {
    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 + "");
            arewedone = 0;
        } else if (input.equalsIgnoreCase("B")) {
            choice = (footballTeam2 + "");
            arewedone = 0;
        } else if (input.equalsIgnoreCase("C")) {
            System.out.println("Game Over");
            arewedone++;
        }

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

    return choice;
}

public static void handleTeamScore(FootballTeam team) {

    int points;

    do {
        System.out.println("How many points were scored?");
        System.out.print("?: ");

        points = keyboard.nextInt();

        if ((team.addScore(points)) == true) {
            arewedone++;
        } else {
            System.out.println("That was an invalid option. Please try again.");
            System.out.println("Hints:");
            System.out.println("Touchdown = 6 points");
            System.out.println("Field Goal = 3 points");
            System.out.println("Safety = 2 points");
            System.out.println("Extra Point = 1 point");
        }
    } while (arewedone == 0);
}    

}

这是我的另一个使用addscore方法和getter and setter的类。

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 :(得分:1)

更改方法调用

choice = getMenuChoice(FootballTeam footballTeam1, FootballTeam footballTeam2);

到此:

choice = getMenuChoice(footballTeam1, footballTeam2);

修改

您需要在致电FootballTeam team之前初始化handleTeamScore(team);

team = new FootballTeam(...);
handleTeamScore(team);

更改此getMenuChoice的代码块:

if (input.equalsIgnoreCase("A")) {
            choice = (footballTeam1 + "");
            arewedone = 0;
        } else if (input.equalsIgnoreCase("B")) {
            choice = (footballTeam2 + "");
            arewedone = 0;

到此:

public static String getMenuChoice(...){
...
if (input.equalsIgnoreCase("A")) {
            choice = "footballTeam1";
            arewedone = 0;
        } else if (input.equalsIgnoreCase("B")) {
            choice = "footballTeam2";
            arewedone = 0;
...
}

现在在main中替换这两个语句:

choice = getMenuChoice(footballTeam1, footballTeam2);
handleTeamScore(team);

到此:

choice = getMenuChoice(footballTeam1, footballTeam2);
if(choice != null) {
if(choice.equals("footballTeam1")){
team = footballTeam1;
}

if(choice.equals("footballTeam2")){
team = footballTeam2;
}
handleTeamScore(team);
}

答案 1 :(得分:0)

你错误地调用了你的方法

你应该这样称呼它

choice = getMenuChoice1( footballTeam1,  footballTeam2);

通过这样称呼它

choice = getMenuChoice(FootballTeam footballTeam1, FootballTeam footballTeam2);

您混淆编译器是否正在调用方法或声明新方法。我们不需要在调用方法时键入类型,其隐式始终