在我的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));
我的條件語句不中我的設置方法工作,要麼。不能理解爲什麼那些人不工作,他們非常直截了當。
什麼是'公共靜態字符串的setName的目的; public static double setPrice; public static int setPrioirty;' – vandale
eclipse讓我改變它們爲靜態。我得到一個錯誤,並且對於自動修復,eclipse將所有內容都改爲靜態。 –
擺脫他們。您需要使用Sotirios Delimanolis – vandale