我有一个家庭作业,但我是Java的新手,过去两天一直试图创建一个玩家对象数组,设置名称并获取名称。
但无论我尝试什么,我都会得到错误。我已经观看了很多教程并准确地复制了他们已完成的内容,但它并没有起作用。如何正确创建数组并获取/设置名称?
我有一个Game类和一个Player类:
游戏类 - 一个版本:
Player[] players = new Player[3];
//All the tutorials I've seen show both these types of putting names in:
players[0] = new Player();
//followed by:
players[0].setName("name");
//or...
players[0] = new Player("human");
//my errors on both: unknown class 'players'...
//unexpected token... invalid method... etc etc
Gmae类 - 不同版本(使用方法):
//for the homework, I'm expected to put it all into a method
//ideally I'd be using the setName method from the Players class (below)
//but that doesn't work here
void createPlayers(Player[] players) {
for (Player p : players) {
players[0] = new Player("human");
players[1] = new Player("Greg");
players[2] = new Player("Susan");
}
}
public static void main(String[] args) {
Game obj = new Game();
//I had to change Players[] to static to get a printout but it's not supposed
//to be static
//I tried these separately to check output
//I need to be able to get the name of a player
obj.createPlayers(players); //no output
for (Player pl : players) {
System.out.println("Name = " + pl); //null null null
}
System.out.println(obj.players[0]); //null
obj.player[0].getName(); //cannot resolve symbol 'player'
}
游戏类 - 另一个版本(将所有内容放在主要内容):
public static void main(String args[]) {
Main game = new Main();
Player p0 = new Player("Jeff"); //it errored if I didn't put a name
Player p1 = new Player("Susan"); //but I need to be able to change names
Player p2 = new Player("Michael");
Player[] players = new Player[3];
players[0] = p0;
players[1] = p1;
players[2] = p2;
p2.setPName("Alan");
System.out.println(p2.getPName()); //output: null
玩家类:具有getter和setter功能,我最好使用它来获取和设置玩家名称,但我不知道如何让它们在我的游戏类中工作。
private String name;
Player (String name) { this.name = name; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
答案 0 :(得分:2)
对你的Game类进行了一个简单的修改就给了我这个,这几乎就是你想做的事情:
public class Game {
void createPlayers(Player[] players) {
Player player1 = new Player();
player1.setName("human");
Player player2 = new Player();
player2.setName("Greg");
Player player3 = new Player();
player3.setName("Susan");
players[0] = player1;
players[1] = player2;
players[2] = player3;
}
public static void main(String[] args) {
Player[] players = new Player[3];
Game obj = new Game();
obj.createPlayers(players);
for (Player pl : players) {
System.out.println("Name = " + pl.getName());
}
}
}
答案 1 :(得分:2)
1)对于您的游戏类 - 一个版本
players[0] = new Player();
将无效,因为您的类Player中没有任何defautl构造函数。 您可以添加默认构造函数,您的类将如下所示。
private String name;
Player () {}
Player (String name) { this.name = name; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
2)对于你的游戏类 - 不同的版本 在你的主类中,你还没有创建玩家列表而没有创建列表,你将在方法中传递它。
在之前的主课堂中
obj.createPlayers(players);
添加Player[] players = new Player[3];
3)对于您的游戏类 - 另一个版本
输出为空,因为System.out.println(p2.getPName());
您尝试p2.getPName()
,但在您的类播放器中,getter为getName()
。只需改变它,它就会起作用。
答案 2 :(得分:1)
在第一个列表中,您创建没有名称的新Player,这是错误的,因为在类中使用参数默认构造函数禁用后定义新构造函数后,您需要添加默认构造函数或创建名称为的玩家。
Player[] players = new Player[3];
//我见过的所有教程都显示了这两种类型的推杆名称 在:
players[0] = new Player();
< - 错!因为你没有这个 Player类中的构造函数
代码的另一部分也包含错误,您在Player类中放置一个getter和setter方法名称,但尝试将它们与其他名称一起使用。
class Player {
private String name;
Player (String name) { this.name = name; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
p2.setPName("Alan"); System.out.println(p2.getPName()); <--this method is not exists
正确的代码就是这样,但请注意它的设计不好:
public class Main {
public static void main(String...args) {
Game game = new Game();
Player[] players = game.getPlayers();
for(Player player : players) {
System.out.println(player.getName());
}
for(int i = 0; i < players.length; i++) {
players[i].setName("new name");
}
for(Player player : players) {
System.out.println(player.getName());
}
}
}
class Player {
private String name;
public Player(String name) { this.name = name; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
class Game {
private Player[] players;
public Game() {
players = { new Player("human"), new Player("Greg"), new Player("Susan") };
}
public Player[] getPlayers() {
return players;
}
}
将此代码放在带有 Main.java 名称的文件中,并放在控制台中的同一文件夹中 javac Main.java 之后用 java Main 命令启动你的程序
输出将是:
human
Greg
Susan
new name
new name
new name
答案 3 :(得分:1)
//Player Class . Create a package named test and create a class
package test;
public class Player {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Create a Game class in same package
package test;
public class Game {
//Game Name is just name of the game
private String gameName;
private Player[] players;
public String getGameName() {
return gameName;
}
public void setGameName(String gameName) {
this.gameName = gameName;
}
public Player[] getPlayers() {
return players;
}
public void setPlayers(Player[] players) {
this.players = players;
}
}
//Create a Test class in same package which contains main method
package test;
public class Test {
public static void main(String[] args) {
Game game =new Game();
Player []players = new Player[3];
players[0]= new Player(); //Create Player Object and assign it into
player[0]
players[0].setName("name1");
players[1] = new Player();
players[1].setName("name2");
players[2] = new Player();
players[2].setName("name3");
game.setPlayers(players);
for(Player player : game.getPlayers()){
System.out.println(player.getName());
}
}
}
答案 4 :(得分:0)