簡體   English   中英

如何在對象數組中添加和打印?

[英]how to add and print into and from an object array?

我在做書店系統。 用戶必須選擇是否添加,刪除等。 如果他選擇添加那么他將寫出所有書籍屬性,后來將保存在items數組中“items array is a array object”這是我的代碼

public class userChoices {

    Items[] items = new Items[200];
    AddItem add = new AddItem();
    Scanner s = new Scanner(System.in);

    public void add(){
        boolean invalidInput;
        int q = -1;
        do {        
            try {        
                invalidInput = false;
        System.out.println("How many book(s) you want to add?");
            q = s.nextInt();
            for(int i = 0; i < q; i++){
                add.getCode();
                add.getQuantity();
                add.getCostPrice();
                add.getDescription();
                add.getDiscount();
                add.getSellingPrice();
                add.getStatus();
                for(int r = 0; r < items.length; r++){
                    if(items[r] != null){
                        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
                        continue;
                    }
                }
            }

            } catch (InputMismatchException e) {
        System.out.println("Please enter a valid integer.");
                s.next();
        invalidInput = true;  // This is what will get the program to loop back
            }
    } while (invalidInput);
    }

this is my Items class
    public class Items {
    private int code, quantity;
    private String description;
    private double costPrice, sellingPrice;
    String status, discount;

    public Items(){
        this.code = 1111;
        this.quantity = 1;
        this.description = "Action";
        this.costPrice = 12.00;
        this.sellingPrice = 16.00;
        this.discount = "5%";
        this.status = "Unvailable";

    }

    public Items(int code, String description, int quantity,
            double costPrice, double sellingPrice, String status, String discount){
        this.code = code;
        this.description = description;
        this.quantity = quantity;
        this.costPrice = costPrice;
        this.sellingPrice = sellingPrice;
        this.status = status;
        this.discount = discount;
    }

    public void setCode(int code){
        this.code = code;
    }

    public void setQuantity(int quantity){
        this.quantity = quantity;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public void setcostPrice(double costPrice){
        this.costPrice = costPrice;
    }

    public void setsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
    }

    public void setStatus(String status){
        this.status = status;
    }

    public void setDiscount(String discount){
        this.discount = discount;
    }

    public int getCode(int code){
        this.code = code;
        return this.code;
    }

    public int getQuantity(int quantity){
        this.quantity = quantity;
        return this.quantity;
    }

    public String getDescription(String description){
        this.description = description;
        return this.description;
    }

    public double getcostPrice(double costPrice){
        this.costPrice = costPrice;
        return this.costPrice;
    }

    public double getsellingPrice(double sellingPrice){
        this.sellingPrice = sellingPrice;
        return this.sellingPrice;
    }

    public String getStatus(String status){
        this.status = status;
        return this.status;
    }

    public String getDiscount(String discount){
        this.discount = discount;
        return this.discount;
    }

    public String toString(){
        return ("code : " + this.code + "\nQuantity : " + this.quantity +
                "\nDescription : " + this.description + "\nCost price : " + this.costPrice
                + "\nSelling price : " + this.sellingPrice + "\nstatus : " + this.status
                + "\ndiscount : " + this.discount);
    }


}

當我在我添加后打印項目[0]時,它顯示我“null”

除了if(items[r] != null){由其他人指出,我相信這段代碼中的continue語句是錯誤的:

for(int r = 0; r < items.length; r++){
    if(items[r] != null){
        items[r] = new Items(add.getCode(), add.getDescription(), add.getQuantity(),
                add.getCostPrice(), add.getSellingPrice(), add.getStatus(), add.getDiscount());
        continue;
    }
}

此聲明將在下一次迭代中繼續:

for(int r = 0; r < items.length; r++){

雖然在我看來你希望它繼續下一次迭代

for(int i = 0; i < q; i++){

所以你應該使用break; 而不是continue;


吸氣劑

public double getcostPrice(double costPrice){
    this.costPrice = costPrice;
    return this.costPrice;
}

這個“getter”本質上是一個setter,它返回你剛剛設置的值,這不是getter應該做的。

public double getcostPrice(){
    return this.costPrice;
}

這就是吸氣劑應該如何工作的方式。

你的錯誤在於這一行:

                    if(items[r] != null){

創建數組時,由於它是一個對象引用數組,因此它的所有元素都為null。 所以這個條件導致它永遠不會將項插入數組。

無論如何你不應該有一個循環。 您應該保留數組中下一個空位的索引,並使用它將該項放入數組中,然后遞增(添加一個)它。

暫無
暫無

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

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