簡體   English   中英

Java計數器計數不正確

[英]Java counter not counting properly

嗨,我是Java的新手,遇到這個硬件問題,我們被要求構建Class LTile,這是拼字游戲中的一塊牌。 塊的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().

塊的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而不是靜態計數器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM