2013-10-08 37 views
-1

在我的for循环结束时,Id想要打印出数组中的所有对象。我用从源字符串生成一个生成的toString,然而,循环完成执行后,它打印出变量项目的默认值:()@override to.String只打印出默认构造函数java hw

[项目[用getPrice()= 0.0,的getName =否()= - 1.0],Item [getPrice()= 0.0,getName()= No Name yet。,getPriority()= - 1.0],Item [getPrice()= 0.0,getName()= No ()= - 1.0],Item [getPrice()= 0.0,getName()= No Name yet。,getPriority()= - 1.0],Item [getPrice()= 0.0,getName()= No 。名字呢,getPriority()= - 1.0],项[用getPrice()= 0.0,的getName()=没有名字,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)); 

我的条件语句不中我的设置​​方法工作,要么。不能理解为什么那些人不工作,他们非常直截了当。

+0

什么是'公共静态字符串的setName的目的; public static double setPrice; public static int setPrioirty;' – vandale

+0

eclipse让我改变它们为静态。我得到一个错误,并且对于自动修复,eclipse将所有内容都改为静态。 –

+0

摆脱他们。您需要使用Sotirios Delimanolis – vandale

回答

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)); 
    } 
} 
+0

谢谢你清理它。我知道我在正确的轨道上。再次感谢。 –

+0

@ user2803251我建议你看看[Java Visualize](http://cscircles.cemc.uwaterloo.ca/java_visualize/#)它提供了图形化调试。但请注意'System.in'中的'Scanner'不起作用,因此您将不得不对所有输入进行硬编码 – vandale

1

在你的病情

j < 0 && j > 100 

怎样才能既j0100小和更大?您需要||

在你的方法

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类的static领域,例如不字段。请使用您拥有的setter或使用构造函数。例如

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方法。 Go through this tutorial.

+0

对,需要或者而不是和。 –

+1

你是指过度使用它们? –

+0

,因为它是一项家庭作业,所以很可能要求操作者实施和使用设定器,以至于他是使用设定器所需的 – vandale