【问题标题】:How to initialize an array of objects and pass an array of strings?如何初始化对象数组并传递字符串数组?
【发布时间】:2018-04-16 23:04:09
【问题描述】:

我正在为一个学校项目创建一个基于文本的骰子游戏。下面的代码旨在如下工作:

  1. 提示用户玩游戏的玩家数量。
  2. 创建一个字符串数组并提示用户输入玩家姓名并相应地填充该数组。
  3. 创建一个 qwixx 对象并将玩家姓名的字符串数组传递给它。
  4. 在 qwixx 构造函数中创建一个播放器对象数组。
  5. 循环遍历对象数组并将字符串数组中的玩家名称传递给每个对象。
  6. 使用 getName() 方法打印出所有玩家的姓名。

我目前遇到的错误是在 play() 方法中指出“玩家”和“玩家”都无法解析为变量。对于 OOP 语言的新手,我们不胜感激。

import java.util.Scanner;

    public class driver {

        public static void main(String[] args) {

        Scanner input_scanner = new Scanner(System.in);
        System.out.print("Please enter the number of players (2-5): ");
        numPlayers = input_scanner.nextInt();
        String players[] = new String[numPlayers];
        for (int x = 0; x < numPlayers; x++) {
            System.out.print("Please enter the name of Player" + (x+1) + ": ");
            players[x] = input_scanner.next();
        }
        qwixx game = new qwixx(players);
        game.play();

        input_scanner.close();

        }

    }

public class qwixx {

    public qwixx(String[] players) {
        player[] Players = new player[players.length];
        for (int x = 0; x < players.length; x++) {
            Players[x] = new player(players[x]);
        }
    }

    public void play() {
        for (int i = 0; i < players.length; i++) {
            System.out.println(Players[i].getName());
        }
    }

}

public class player {

    public player(String playerName) {
        name = playerName;
    }

    public String getName() {
        return name;
    }

}

【问题讨论】:

  • 好吧,你的 qwixx 类中没有 players 对象..
  • player[] Players: 顺便说一句,你的约定被颠覆了。
  • @austinwernli “玩家”(小写 p)是保存玩家姓名的字符串数组。
  • 我在某些方法中看到了这一点。但是,您需要研究变量范围,因为 play() 无法在任何地方访问 players 变量
  • @S.Miller 不,类名应该使用 PascalCase,变量名应该使用 camelCase。

标签: java oop object


【解决方案1】:

您的代码存在一些问题。但最重要的一点是,在除 main 类之外的每个类中,您确实没有声明任何属于该类的变量。不要在构造函数中引入类变量。在构造函数之外声明它们,以便它们可以是全局变量。试试:

public class qwixx {

    player[] Players;     //declared before using it in any methods / constructor

    public qwixx(String[] players) {
        Players = new player[players.length];    //value initialized inside the constructor
        for (int x = 0; x < players.length; x++) {
            Players[x] = new player(players[x]);
        }
    }

    public void play() {
        for (int i = 0; i < Players.length; i++) {
            System.out.println(Players[i].getName());
        }
    }
}

同样,尝试在你的播放器类中做同样的事情:

public class player {

    String name;

    public player(String playerName) {
        name = playerName;
    }

    public String getName() {
        return name;
    }

}

在你的 main 上,你忘记将 numPlayers 声明为 int。也这样做。

int numPlayers = input_scanner.nextInt

【讨论】:

    【解决方案2】:

    首先,您已经在 main 方法中实例化了大部分数据,导致您的操作代码是静态的! 其次,以下行未声明为全局“但需要使用 static”,因此只能在静态 main 方法中看到。

    String players[] = new String[numPlayers];
    

    在主方法中使用的“public class qwixx”应该是“public static class qwixx” 下面是一个“新实例”,不是静态的

    qwixx game = new qwixx(players);
    

    但是调用它的问题如下,因为一个静态类没有被声明为新的,它只是用一个需要使用的方法来调用

    qwixx.play();

    更重要的是,任何类名最好按照其以大写字母开头的约定来编写,以帮助阅读代码和编译器不被绊倒。例如"公共静态类 Qwixx"

    “删除静态上下文”或完全在其中工作,仅一种或另一种。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2020-04-21
      相关资源
      最近更新 更多