-1

在我的 for 循环结束时,我想打印出数组中的所有对象。我使用了来自 Source 的带有字符串生成器的生成 toString,但是,在循环完成执行后,它会打印出变量 Item 的默认值:

[Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0] , Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0] , Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0], Item [getPrice()=0.0, getName()=No Name yet., getPriority()=-1.0] , 空值]

这是我的代码

  public class Item {


static Item list[]=new Item [7];
public static  int x = 0;
public static  String setName;
public static double setPrice;
public static int setPrioirty;


private  int priority=-1;
private double price;
private String name;



Item(){

    priority=-1;
    price=0;
    name="No Name yet.";


}// default constructor. 


public Item(int i, double j, String k) {
    setItem(i,j,k);                         //constructor with 3 arguments. 
}

public void setItem (int i, double j, String k){    // setting item with 3 attributes.
    setPriority(i);
    setPrice(j);
    setName(k); 
}

public void setName(String k) { //setting individual attributes in item.

    // TODO Auto-generated method stub //page 378
    name=k;

}


public void setPrice(double j) {//setting individual attributes in item.
    // TODO Auto-generated method stub
    if (j<0||j>100){
        System.out.println("Error: price is too low or high");

    }

    else
        price=j;

    }

public void setPriority(int i) {//setting individual attributes in item.
    // TODO Auto-generated method stub
    priority =((i>=0&&i<7)?i:0);

}


public double getPrice(){
    return price;

}
public String getName(){

    return name;

}
public double getPriority(){
    return priority;

}


 public static void add(Item itemObject) {


    if (x<7)
    {
        list[x]=itemObject;
    System.out.println("Item added at index " + x);

    x++;


    }


 }


@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Item [getPrice()=").append(getPrice()).append(", ");
    if (getName() != null)
        builder.append("getName()=").append(getName()).append(", ");
    builder.append("getPriority()=").append(getPriority()).append("]");
    return builder.toString();
}

   }

主要的

       import java.util.Arrays;
       import java.util.Scanner;
       import java.util.Set;


     public class homework3main extends Item {



@SuppressWarnings("static-access")
public static void main(String[] args) {

    //item list[]=new item [7]; // array of objects
    Scanner keyboard= new Scanner(System.in);
    for(int x=1; x<7;x++){

        Item itemObject=new Item ();
        //Item itemObject=new Item (setPrioirty,setPrice,setName);
        //creating new object with 3 variables, name, price, priority

        //list[x]=new Item();// is this right?
        System.out.println("Enter an item you want to add to your list "+ x);
        list[x].setName=keyboard.next();

        System.out.println("Enter a price "+x);
        list[x].setPrice=keyboard.nextDouble();

        System.out.println("Enter the priority of the item "+x);
        list[x].setPrioirty=keyboard.nextInt();

        //item itemObject=new item (setPrioirty,setPrice,setName);

        list[x].add(itemObject);

    }   
    System.out.println(Arrays.toString(list));

我的条件语句在我的 Set 方法中也不起作用。无法理解为什么这些不起作用,它们非常简单。

4

2 回答 2

2

您的代码似乎有几个结构性问题,所以我认为应该是这样:

import java.util.Arrays;
import java.util.Scanner;

public class Item {
    //the properties of an Item

    private int priority;
    private String name;
    private double price;

    //default constructer
    public Item() {
        priority = -1;   //fill with default values
        price = 0.0;
        name = "No name yet";
    }
    //constructer with all fields given

    public Item(int priority, String name, double price) {
        this.priority = priority; //there are two instances of each variable
        this.name = name;         // use 'this.' to distinguish them
        this.price = price;
    }
    // all getters simply will return the corresponding field

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        //priority must be between 0 and 7
        if (priority >= 0 && priority <= 7) {
            this.priority = priority;
        } else {
            //otherwise default to 0
            this.priority = 0;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //no constraints on the name so simply assign it
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        //price between 0 and 100 inclusive
        if (price >= 0) {
            if (price <= 100) {
                this.price = price;
            } else {
                //use System.err for errors
                // used nested ifs so you can tell if price is to high or low 
                //otherwise it is a bit ambiguous
                System.err.println("Error: price to high");
            }
        } else {
            System.err.println("Error: price to low");
        }
    }
    //your tostring is fine
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Item [getPrice()=").append(getPrice()).append(", ");
        if (getName() != null) {
            builder.append("getName()=").append(getName()).append(", ");
        }
        builder.append("getPriority()=").append(getPriority()).append("]");
        return builder.toString();
    }

    //just put your main here
    //if you can't then put it in a class but don't sub-class this class
    public static void main(String[] args) {
        //put your list declaration here
        //it doesn't quitemake sense for the Item calss to have a field
        //called list in this instance
        Item[] list = new Item[7];
        Scanner keyboard = new Scanner(System.in);
        //i is the most commonly used variable for 'for' loops
        for (int i = 1; i <= list.length; i++) {
            //create a new item
            Item anItem = new Item();
            //call your methods on that object to set its fields
            System.out.println("Enter an item you want to add to your list " + i);
            anItem.setName(keyboard.next());

            System.out.println("Enter a price " + i);
            anItem.setPrice(keyboard.nextDouble());

            System.out.println("Enter the priority of the item " + i);
            anItem.setPriority(keyboard.nextInt());

            //use the var i for the position
            //remember to subtract 1 since arrays start at 0 but i starts at 1
            list[i-1] = anItem;
        }
        System.out.println(Arrays.toString(list));
    }
}
于 2013-10-08T02:26:16.273 回答
1

在你的情况下

j < 0 && j > 100

如何j既小于0又大于100?你需要||.

在你的方法中

System.out.println("Enter an item you want to add to your list "+ x);
list[x].setName=keyboard.next();

System.out.println("Enter a price "+x);
list[x].setPrice=keyboard.nextDouble();

System.out.println("Enter the priority of the item "+x);
list[x].setPrioirty=keyboard.nextInt();

您正在设置类的static字段Item,而不是实例的字段。使用您拥有的设置器或使用构造函数。例如

Item itemObject = new Item ();
System.out.println("Enter an item you want to add to your list "+ x);
itemObject.setName(keyboard.next());

System.out.println("Enter a price "+x);
itemObject.setPrice(keyboard.nextDouble());

System.out.println("Enter the priority of the item "+x);
itemObject.setPriority(keyboard.nextInt());

list[x] = itemObject;

顺便说一句,你完全过度使用了 setter。浏览本教程。

于 2013-10-08T01:25:07.003 回答