创建一个对象数组但它找不到符号

问题描述 投票:1回答:1

我用Java创建了一个小游戏玩家阵列。有一个名为Players的类和一个名为Game的类。

Main,我们扫描两个名字并将它们发送到Game game.createPlayer(name1, name2); 然后尝试回复一些信息 playerArray[(game.getPlayerTurn() % 2)].getPlayerName();

PlayerGame中构造为一个数组:

public class Game
{
private Player[] playerArray;
[...]
public void createPlayer(String name1, String name2)
  {
    Player[] playerArray = new Player[2];        
    playerArray[0] = new Player(name2);           
    playerArray[1] = new Player(name1);
  }

Player作为标准类:

public class Player
{
  private String playerName;

  public Player( String playerName )
  {
    this.playerName = playerName;
  }
  public String getPlayerName()
  {
    return playerName;
  }
}

然而,这会返回多个错误,表示无论我在哪里找到玩家的名字,它都找不到符号。我没有正确地安抚他们吗?

附加代码(根据要求):

package oop.nimspiel;

import java.util.Scanner;
import java.util.Arrays;

public class Game
{

  private int take;
  private int turn;
  private int playerTake;
  private int playerTurn;
  protected Player[] playerArray;

  public Game(int turn, int playerTurn)
  {
    this.turn = turn;
    this.playerTurn = playerTurn;
  }

  protected void setPlayerTake(int take)
  {
    this.playerTake = take;
  }

  public int getPlayerTake()
  {
    return playerTake;
  }

  public void incrementTurns()
  {
    turn = turn + 1;
    playerTurn = playerTurn + 1;
  }

  public int getTurn()
  {
    return turn;
  }

  public int getPlayerTurn()
  {
    return playerTurn;
  }

  public void createPlayer(String name1, String name2)
  {
    this.playerArray = new Player[2];
    playerArray[0] = new Player(name2);
    playerArray[1] = new Player(name1);
  }

  public String getPlayer()
  {
    String playerName = playerArray[(getPlayerTurn() % 2)].getPlayerName();
    return playerName;
  }

  public void checkTake(int take)
  {
    Scanner input = new Scanner(System.in);

    this.take = take;
    boolean rightInput = false;

    do {

      if (take < 1 || take > 3)
      {
        System.out.println("Your input was wrong, please use a number between 1 and 3.");

        System.out.println("How many stones would you like to take?");
        take = input.nextInt();

        rightInput = false;

      }

      else if (stoneheap.getStones() < take) {
        System.out.println("There are only " + stoneheap.getStones() + " stones left.");
        System.out.println("Please take less.");

        System.out.println("How many stones would you like to take?");
        take = input.nextInt();

        rightInput = false;
      }

      else
      {
        rightInput = true;
      }

    } while (rightInput == false);
  }

}

和主要:

package oop.nimspiel;

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class Main
{

  private int take;

  public static void main(String[] args)
  {

    Scanner input = new Scanner(System.in);

    String nextRound;

    do
    {

      int maxPlayers = 2;
      int startTurn = 1;
      Game game = new Game ( startTurn, (1 + (int)(Math.random() * ((maxPlayers - 1) + 1)) ) );

      int minStones = 20;
      int maxStones = 30;
      Stoneheap stoneheap = new Stoneheap((minStones + (int)(Math.random() * ((maxStones - minStones) + 1)) ) );

      System.out.println("Rules: Two players take stones from a heap of 20 to 30 until there are no more left. The one to take the last stone loses. Each round you can only take between 1 - 3 stones. Have fun!");   // Rules

      System.out.println("");

      System.out.println("Hello Player 1, what is your name?");
      String name1 = input.next();

      System.out.println("");

      System.out.println("Hello Player 2, what is your name?");
      String name2 = input.next();

      game.createPlayer(name1, name2);

      System.out.println("");

      System.out.println("Number of stones: " + stoneheap.getStones());

      System.out.println("The first to draw is Player " + game.getPlayerTurn());

      System.out.println("The game starts now!");

      while (stoneheap.getStones() > 0)
      {
        if ((game.getPlayerTurn() % 2) > 0)         // Turn Player 1
        {
          System.out.println("It is your turn " + playerArray[(game.getPlayerTurn() % 2)].getPlayerName() + ".");
          System.out.println("How many stones would you like to take?");

          int take = input.nextInt();

          game.checkTake(take);

          game.setPlayerTake(take);
          stoneheap.currentStones();

          System.out.println("There are  " + stoneheap.getStones() + " stones left.");

        }

        else    // Turn Player 2
        {
          System.out.println("It is your turn " + playerArray[(game.getPlayerTurn() % 2)].getPlayerName() + ".");
          System.out.println("How many stones would you like to take?");

          int take = input.nextInt();

          game.checkTake(take);

          game.setPlayerTake(take);

          stoneheap.currentStones();

          System.out.println("There are  " + stoneheap.getStones() + " stones left.");

        }

        game.incrementTurns();
      }

      System.out.println("The game has ended and the winner is ...");
      System.out.println(playerArray[(game.getPlayerTurn() % 2)].getPlayerName());

      System.out.println("It took " + (game.getTurn() - 1) + " turns." );

      System.out.println("");


      System.out.println("Do you want to play another round? Y for yes, anything else for no");

      String userInput = input.next();

      nextRound = userInput.toUpperCase();

    } while (nextRound.equals("Y"));

  }

}
java arrays object instance
1个回答
0
投票

在你的createPlayer方法中,你应该通过playerArray关键字(this)访问this.playerArray = new Player[2]

目前,您正在动态创建数组,并且类变量未受影响,这就是您获得异常的原因。

public class Game
{
private Player[] playerArray;
[...]
public void createPlayer(String name1, String name2)
  {
    this.playerArray = new Player[2];        
    playerArray[0] = new Player(name2);           
    playerArray[1] = new Player(name1);
  }
© www.soinside.com 2019 - 2024. All rights reserved.