得到一些我不知道如何修复此方法的错误

时间:2013-11-09 02:13:19

标签: java methods runtime-error

我在这里做错了什么?我在getMenuChoice()方法上遇到错误,我试图根据输入的字母设置我的用户选择= footballTeam1和footballTeam2。在该方法中,我应该向用户打印选项菜单,并使用扫描仪键盘接受用户选择。如果那时用户没有选择列出的选项重新打印菜单并再次询问,我应该将用户的选择作为字符串返回。这是我的代码。

 import java.util.Scanner;

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




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();
    score = handleTeamScore(FootballTeam 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);

}

}

1 个答案:

答案 0 :(得分:0)

您正在尝试在字符串中分配两个不兼容的对象 如果你真的想这样做,你必须先使用对象类型进行强制转换。