简体   繁体   English

垄断游戏的发展

[英]Development of monopoly game

I seem to have a problem with a subtask. 我似乎对子任务有问题。 It's in danish so I put in the translated version of it: 它是丹麦文,所以我输入了它的翻译版本:

  • Create a class Field , that is representing the fields of a monopoly game. 创建一个Field类,它表示垄断游戏的字段。 Initially, Field can contain these encapsulated variables: 最初, Field可以包含以下封装的变量:
    • String name - short name of the field String name -字段的简称
    • int number - a number in the range[1..40] int number number-范围为[1..40]的数字

Both variables must be initialized in a constructor, and there must only be getters, as they never will be changed after creation. 这两个变量都必须在构造函数中初始化,并且只能有吸气剂,因为它们在创建后将永远不会更改。 Moreover, there should be a method with the signature public String toString() , so it's easy to print what Field a player has landed on. 而且,应该有一个带有签名public String toString() ,因此可以很容易地打印出玩家登陆的Field At first it's allowed to just call the fields Field1, Field2... 首先,可以只调用字段Field1,Field2 ...

My Field class look like this: 我的Field类如下所示:

public class Field {

    String name;
    int number;

    public Field(String name, int number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }
}

In my main method I wanted to test this. 在我的main方法中,我想对此进行测试。 So I wrote the following: 所以我写了以下内容:

Field[] board = new Field[40]; // a board containing 40 fields

for (int i = 0; i < board.length; i++) {
    board[i] = new Field("Field" + (i + 1), i + 1);
}

System.out.println("Board: " + Arrays.toString(board));

In my console I get this: 在我的控制台中,我得到以下信息:

Board: [test.Field@2a139a55, test.Field@15db9742, test.Field@6d06d69c,......] 董事会:[test.Field @ 2a139a55,test.Field @ 15db9742,test.Field @ 6d06d69c,......]

And I want this: 我想要这个:

Board: [Field1, Field2, Field3,......] 董事会:[Field1,Field2,Field3,...]

Override Field 's toString() to return the name, ie 覆盖FieldtoString()以返回名称,即

public String toString() {
  return name;
}

What you get (eg test.Field@2a139a55 ) is the default implementation of toString() which can be found in Object : 您得到的内容(例如test.Field@2a139a55 )是toString()的默认实现,可以在Object找到:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

You missed the 你错过了

Moreover, there should be a method with the signatur public String toString(), 此外,应该有一个方法,其签名为public String toString(),

part of your task. 您的任务的一部分。

Are you able to use java8? 您可以使用java8吗? Then I would suggest this: 然后我建议:

Field[] board = new Field[40]; // a board containing 40 fields

for(int i = 0; i < board.length; i++){
    board[i] = new Field("Field" + (i + 1), i + 1);
}

String commaSeparatedName =
    Arrays.stream(board) // all items as stream
          .map(Field::getName) // for each take its name
          .collect(Collectors.joining(", "); // join names with a comma
System.out.println("Board: [" + commaSeparatedNames +"]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM