将用户输入放入数组索引中的某个点

时间:2017-01-18 23:02:43

标签: java arrays

我试图从HockeyPlayer类中的HockeyPlayer构造函数获取用户输入,以存储在数组" player"中的HockeyPlayerMain类中的数组中。每当他们按下" A"它将调用构造函数,如果他们输入数字#6,我需要将对象/实例放在数组索引#5中,但我不知道如何做到这一点,非常初学数组。

很抱歉,如果不清楚,但我可以尝试清理。

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;
private int gameNum;

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(){
    HockeyPlayer player1 = new HockeyPlayer();

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

}

}

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":
           for(int i = 0; i < players.length; ++i){
            players[i] = new HockeyPlayer();
    }
            break;
        case "G":

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

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



}

2 个答案:

答案 0 :(得分:2)

在你的for循环中试试这个:

for(int i = 0; i < players.length; ++i){
        HockeyPlayer player = new HockeyPlayer();    
        players[player.getPlayerNmumber()] = player; 
    }

如果你想一次只输入一个玩家,那么&#39; A&#39;按下选项,然后您只需删除for循环:

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

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

答案 1 :(得分:1)

我不确定这是不是您的意思,但您可以使用getPlayerNumber()类中的HockeyMain方法。如果用户输入不正确,这可能会导致数组中的覆盖,这意味着数组不会被完全填充。

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

switch(choice){
    case "A":
       for(int i = 0; i < players.length; ++i){
        HockeyPlayer p = new HockeyPlayer();
        players[p.getPlayerNumber()] = p;
       }
        break;
    case "G":

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