如何初始化对象数组并传递字符串数组?文本骰子游戏开发问询
Hey there! Let's walk through exactly how to set up the object array initialization and string array passing you need for your text-based Qwixx dice game. I'll use Java for the example (since your description mentions constructors and object arrays, this is the most common language for this kind of school project).
Step 1: Define the Player Class
First, create a simple Player class that holds a player's name and has a constructor to accept that name:
public class Player { private String name; // Constructor that takes a player's name public Player(String playerName) { this.name = playerName; } // Optional: Getter to access the name later public String getName() { return name; } }
Step 2: Build the Qwixx Class with Object Array Initialization
Next, your Qwixx class will take a string array of player names in its constructor, then initialize a Player object array by iterating over that string array:
public class Qwixx { private Player[] players; // Constructor that accepts the string array of player names public Qwixx(String[] playerNames) { // Initialize the Player array to match the length of the input string array this.players = new Player[playerNames.length]; // Iterate over each name and create a Player object for it for (int i = 0; i < playerNames.length; i++) { this.players[i] = new Player(playerNames[i]); } } // Optional: Method to verify players are initialized correctly public void printPlayers() { System.out.println("Game players:"); for (Player player : players) { System.out.println("- " + player.getName()); } } }
Step 3: Main Game Flow (Collecting Input & Initializing Everything)
Now put it all together in your main program, where you collect user input for player count and names:
import java.util.Scanner; public class QwixxGameLauncher { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Get number of players System.out.print("Enter number of players: "); int playerCount = scanner.nextInt(); scanner.nextLine(); // Consume the leftover newline to avoid input issues // Step 2: Create string array and collect player names String[] playerNames = new String[playerCount]; for (int i = 0; i < playerCount; i++) { System.out.print("Enter name for player " + (i + 1) + ": "); playerNames[i] = scanner.nextLine(); } // Step 3: Initialize Qwixx object with the player names array Qwixx game = new Qwixx(playerNames); // Test to make sure players are set up correctly game.printPlayers(); scanner.close(); } }
Key Explanations
- When you pass the
playerNamesstring array to theQwixxconstructor, we first create aPlayerarray of the same length to match the number of players. - The loop in the
Qwixxconstructor takes each name from the string array and uses it to instantiate a newPlayerobject, storing it in the corresponding index of theplayersarray. - The
Scannercode handles collecting user input properly—note thescanner.nextLine()afternextInt()to avoid skipping the first name input (a common gotcha with Java's Scanner).
This setup exactly matches the logic you outlined: collect player names into a string array, pass that array to the Qwixx constructor, and initialize the Player object array from there.
内容的提问来源于stack exchange,提问作者JSON_Derulo

