Java计数器计数不正确

时间:2015-04-22 03:32:56

标签: java counter

您好我是Java的新手并遇到这个硬件问题,我们被要求构建Class LTile,这是Scrabble游戏中的一个区块。 块的ID号应由类/静态数据成员分配,该成员跟踪生成的LTile对象的数量。 我的ID输出没有从1到26打印,而是全部分配26。 我怀疑我的ID属性一定是错的,但无法弄清楚确切的错误。任何帮助是极大的赞赏!谢谢!

package hw2;

    public class LTile {
        char letter;
        private int value;
        private static int ID=0;

        public LTile(){
        this.letter = '?';
        this.value = 0;
        LTile.ID++;
        }
        public LTile(char letter, int value){
            this.letter=letter;
            this.value=value;
            LTile.ID++;
        }
        public char getLetter(){
            return this.letter;
        }
        public int getValue(){
            return this.value;
        }
        public int getID(){
            return LTile.ID;
        }
        public boolean equals(Object obj){
            if(this ==obj){
                return true;}
            else{
                return false;
            }
        }

        public String toString(){
            return "["+ID+":" + letter+","+value+"]";
        }
          //**
          //** main() for testing LTile
          //**
          public static void main(String[] args)
          {
            final String letters = "EAIONRTLSUDGBCMPFHVWYKJXQZ";
            final int[] values  =  {1,1,1,1,1,1,1,1,1,1,2,2,3,3,3,3,4,4,4,4,4,5,8,8,10,10};

            java.util.List<LTile> lst = new java.util.ArrayList<LTile>();
            for (int i = 0; i < letters.length(); ++i)
              lst.add(new LTile(letters.charAt(i), values[i]));

            for (LTile tile : lst)
              System.out.println(tile);
            System.out.println();

            // test for equals
            boolean found = false;
            for (int i = 0; i < lst.size()-1; ++i) {
              for (int j = i+1; j < lst.size(); ++j) {
                if (lst.get(i).equals(lst.get(j))) {
                  System.out.println("ERROR in equals() found for "
                      + lst.get(i) + " and " + lst.get(j));
                  found = true;
                }
              }
            }
            if (!found)
              System.out.println("No error in equals().");
          }
    }

My output is:
    [26:E,1]
    [26:A,1]
    [26:I,1]
    [26:O,1]
    [26:N,1]
    [26:R,1]
    [26:T,1]
    [26:L,1]
    [26:S,1]
    [26:U,1]
    [26:D,2]
    [26:G,2]
    [26:B,3]
    [26:C,3]
    [26:M,3]
    [26:P,3]
    [26:F,4]
    [26:H,4]
    [26:V,4]
    [26:W,4]
    [26:Y,4]
    [26:K,5]
    [26:J,8]
    [26:X,8]
    [26:Q,10]
    [26:Z,10]

    No error in equals()

    **The correct output should be:**
    [1: E,1]
    [2: A,1]
    [3: I,1]
    [4: O,1]
    [5: N,1]
    [6: R,1]
    [7: T,1]
    [8: L,1]
    [9: S,1]
    [10: U,1]
    [11: D,2]
    [12: G,2]
    [13: B,3]
    [14: C,3]
    [15: M,3]
    [16: P,3]
    [17: F,4]
    [18: H,4]
    [19: V,4]
    [20: W,4]
    [21: Y,4]
    [22: K,5]
    [23: J,8]
    [24: X,8]
    [25: Q,10]
    [26: Z,10]

    No error in equals().

1 个答案:

答案 0 :(得分:3)

  

磁贴的ID号应由类/静态数据成员分配,该成员跟踪生成的LTile对象的数量。

这意味着id值应来自静态数据成员,而不应该是静态数据成员。因此,您需要两个字段:一个实例id字段用于保存对象的ID,一个静态CURRENT_ID字段用于跟踪您到目前为止创建的对象数。

public class LTile {
    char letter;
    private int value;
    private int id; // instance id
    private static int CURRENT_ID = 0; // static counter from where the instance ids will be drawn

    public LTile() {
        // Call the other constructor to avoid unnecessary repetition
        this('?', 0);
    }

    public LTile(char letter, int value) {
        this.letter = letter;
        this.value = value;
        this.id = LTile.CURRENT_ID++;
    }

    // rest of the code

    public int getID() {
        return this.id;
    }

    public String toString() {
        return "["+this.id+":" + this.letter+","+this.value+"]";
    }
}

请注意,您还需要更改getId()toString()以使用实例id而不是静态计数器。