Java 计数器计数不正确

标签 java counter

您好,我是 Java 的新手,遇到了这个硬件问题,我们被要求构建 LTile 类,这是拼字游戏中的一个拼贴 block 。 tile 的 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().

最佳答案

The ID number of a tile should be assigned by a class/static data member, which keeps track of the number of LTile objects produced.

这意味着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 而不是静态计数器。

关于Java 计数器计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787223/

相关文章:

javascript - jquery 文本区域值计数长度

java - JShell <Shift+tab i> 在 jdk 9 中无法正常工作

java - 检查 Web 代理的类型

java - 使用 spring 在属性文件中设置值

Python子类计数器

python - forloop.counter 表示 for 标签已经循环了多少次

java - 为什么在 spring security 中不在 FilterChainProxy 中的过滤器上调用 init()?

java - 检测输入流是否放气

java - 在 Java 中将计数器重置为零?

java - 在另一个类的方法上使用count方法