【问题标题】:Passing an object to a switch statement to be passed to a method in different class将对象传递给 switch 语句以传递给不同类中的方法
【发布时间】:2017-01-18 23:59:22
【问题描述】:

我正在尝试将 case A 中的对象传递给 case G,并尝试将其传递给 HockeyPlayer 类中的方法 addGameDetails()

以便我添加的A 的玩家可以获取有关它们的详细信息。

package hockeyplayer;

import java.util.Scanner;

public class HockeyPlayer {

    private final int[] goals = new int[10];
    private final String[] teamName = new String[10];
    private final int[] gameNumber = new int[10];
    private int[] game = new int[10];
    private static String name;
    private static int playerNumber;

    public HockeyPlayer() {
        Scanner input = new Scanner(System.in);
        int goalsScored = -1;
        System.out.println("Enter the name of the player");
        name = input.nextLine();
        System.out.println("Enter the player number 1-12");
        playerNumber = input.nextInt();

    }

    public int[] getGoals() {
        return goals;
    }

    public String[] getTeamName() {
        return teamName;
    }

    public int[] getGameNumber() {
        return gameNumber;
    }

    public int[] getGame() {
        return game;
    }

    public static String getName() {
        return name;
    }

    public static int getPlayerNumber() {
        return playerNumber;
    }

    public void setGame(int[] game) {
        this.game = game;
    }

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

    public static void setPlayerNumber(int playerNumber) {
        HockeyPlayer.playerNumber = playerNumber;
    }



    public void addGameDetails() {
        HockeyPlayer firstPlayer = new HockeyPlayer();
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the game number");
        gameNumber [0] = input.nextInt();
        System.out.println("What team was it against?");
        teamName[0] = input.nextLine();
        System.out.println("How many goals was scored by "+name+"?");
        goals[0] = input.nextInt();


    }

    @Override
    public String toString() {
        String output = "";

        return output;
    }

}

package hockeyplayer;

import java.util.Scanner;

public class HockeyPlayerMain {

    private static HockeyPlayer[] player = new HockeyPlayer[12];

    private static final String MENU = "***Hockey Tracker***\n"
            + "A- Add player details\n"
            + "G- Add game details\n"
            + "S- Show players \n"
            + "X- Exit\n";

    public static void main(String[] args) {
        String option = "";
        Scanner input = new Scanner(System.in);

        do {
            System.out.println(MENU);
            option = input.nextLine();
            switch (option) {
                case "A":
                    HockeyPlayer p = new HockeyPlayer();
                    player[p.getPlayerNumber() - 1] = p;

                    break;

                case "G":

                    break;

                case "S":

            }

        } while (!option.equalsIgnoreCase("X"));

    }
}

【问题讨论】:

  • 保持对您在A 中创建的最后一个播放器的引用作为main 中的局部变量,只是不要忘记在您之前检查lastPlayer 是否不是null尝试从中获取信息

标签: java arrays class methods switch-statement


【解决方案1】:

如果您想为任何 HockeyPlayer 添加GameDetails,您可以像这样询问 playerNumber:

public static void main(String[] args) {
    String option = "";
    Scanner input = new Scanner(System.in);

    do {
        System.out.println(MENU);
        option = input.nextLine();
        switch (option) {
            case "A":
                HockeyPlayer p = new HockeyPlayer();
                player[p.getPlayerNumber() - 1] = p;

                break;

            case "G":
                int playerId = Integer.valueOf(input.nextLine());
                player[playerId-1].addGameDetails();
                break;

            case "S":

        }

    } while (!option.equalsIgnoreCase("X"));

}

或者如果您只想传递最后添加的内容,您可以保存参考:

public static void main(String[] args) {
    String option = "";
    Scanner input = new Scanner(System.in);
    HockeyPlayer lastAdded = null; 

    do {
        System.out.println(MENU);
        option = input.nextLine();
        switch (option) {
            case "A":
                HockeyPlayer p = new HockeyPlayer();
                player[p.getPlayerNumber() - 1] = p;
                lastAdded = p;
                break;

            case "G":
                if (lastAdded != null) lastAdded.addGameDetails();
                break;

            case "S":

        }

    } while (!option.equalsIgnoreCase("X"));

}

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2017-09-13
    • 2021-04-28
    相关资源
    最近更新 更多