如何将对象从一个case语句传递给另一个case语句

时间:2017-01-21 21:17:34

标签: java arrays object switch-statement instance

我试图让#34;播放器"来自案例" A"案件" G"但当然我无法做到这一点,因为对象是在案例A中创建的,而我必须将其保留在那里。如何在保持相同实例的同时将其移动到那里?初学者到java。我希望能够通过按" G"

来编辑阵列中的播放器
package hockeyplayer;
import java.util.Scanner;

public class HockeyMain {    
private static String choice;
private static HockeyPlayer[] players = new HockeyPlayer[12];
private static final String MENU = "Hockey Tracker\n"+
        "A-Add Player\n"+
        "G-Add game details\n"+
        "S-Show players\n"+
        "X-Quit\n";

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

do{    
    System.out.println(MENU);
    choice = input.nextLine();

    switch(choice){
        case "A":
           HockeyPlayer player = new HockeyPlayer();    
           players[player.getPlayerNumber()-1] = player; 
            break;
        case "G":



            break;
        case "S":
            break;
        case "X":    
     }

}while(!choice.equals("X"));   
}      
}

package hockeyplayer;
import java.util.Scanner;

public class HockeyPlayer {
private String[] opponent = new String[10];
private int[] goalsScored = new int[10];
private int[] gameNumber = new int[10];
private String name;
private int playerNumber;    

public HockeyPlayer() {    
    Scanner input = new Scanner(System.in);
    System.out.println("What is the name of the player?");
    name = input.nextLine();
    System.out.println("What is the player's number?");
    playerNumber = input.nextInt();
    input.nextLine();    
}

public String[] getOpponent() {
    return opponent;
}

public void setOpponent(String[] opponent) {
    this.opponent = opponent;
}

public int[] getGoalsScored() {
    return goalsScored;
}

public void setGoalsScored(int[] goalsScored) {
    this.goalsScored = goalsScored;
}

public String getName() {
    return name;
}

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

public int getPlayerNumber() {
    return playerNumber;
}

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

public void addGameDetails(){        
    Scanner input = new Scanner(System.in);
    System.out.println("What game number was it?");
    gameNumber[0] = input.nextInt();
    input.nextLine();
    System.out.println("Who were the opponents?");
    opponent[0] = input.nextLine();
    System.out.println("How many goals did the player score?");
    goalsScored[0] = input.nextInt();        
}    
}

2 个答案:

答案 0 :(得分:1)

+-----------------+-------+--------+------+------+-----------------------+ | email_address | brand | region | area | site | lowest_distinct_level | +-----------------+-------+--------+------+------+-----------------------+ | alpha@gmail.com | 1 | 1 | 1 | 2 | area | +-----------------+-------+--------+------+------+-----------------------+ | beta@gmail.com | 1 | 1 | 2 | 0 | region | +-----------------+-------+--------+------+------+-----------------------+ | gamma@gmail.com | 1 | 1 | 2 | 2 | region | +-----------------+-------+--------+------+------+-----------------------+ | zeta@gmail.com | 1 | 2 | 0 | 0 | brand | +-----------------+-------+--------+------+------+-----------------------+ 之外声明select email_address, brand, region, area, site, case when site = 1 then 'site' when area = 1 then 'area' when region = 1 then 'region' when brand = 1 then 'brand' else '' end as lowest_distinct_level from mm; 个实例,如下所示:

HockeyPlayer

我们的想法是switch statement对象应该对所有HockeyPlayer player = null; do{ System.out.println(MENU); choice = input.nextLine(); switch(choice){ case "A": player = new HockeyPlayer(); players[player.getPlayerNumber()-1] = player; break; case "G": player.addGameDetails(); //invoke the method here for case G break; case "S": break; case "X": } }while(!choice.equals("X")); } 都可访问,因此您需要在可以访问的地方声明它。

答案 1 :(得分:1)

player变量的声明移到do-while之外,仍然在case "A"中实例化:

HockeyPlayer player = null;
do {    
  System.out.println(MENU);
  choice = input.nextLine();
  switch(choice){
    case "A":
       player = new HockeyPlayer();    
       players[player.getPlayerNumber()-1] = player; 
        break;
    case "G":
       // you can use player here (assuming 


        break;
    case "S":
        break;
    case "X":    
 }
} while(!choice.equals("X"));