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]时,它显示我&#34; null&#34;
答案 0 :(得分:2)
除了其他人指出的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;
}
这就是吸气剂应该起作用的方式。
答案 1 :(得分:1)
你的错误在于这一行:
if(items[r] != null){
创建数组时,由于它是一个对象引用数组,因此它的所有元素都为null。因此,这种情况会导致它永远不会将项插入数组中。
无论如何你不应该有一个循环。您应该保留数组中下一个空位的索引,并使用它将该项放入数组中,然后递增(添加一个)它。