更新:請閱讀本文的第二部分,其內容爲「更新」。自從原始帖子以來,我已經獲得了部分代碼的部分功能。謝謝!增強for循環未執行
我有一個在它返回一個字符串我toString方法增強的for循環:
public String toString() {
for (SelectedItem item: cartItems) {
return "Item: " + item.getItemNumber() + "\nQuantity: " + item.getQuantity + "\nUnit Price: $" + item.getUnitPrice + "\nTotal Price: $" + (item.getUnitPrice * item.getQuantity);
}
return "";
}
然而,所有我的代碼返回是:
「[]
[]」。我相信我的代碼只執行return「」部分,而不是「return」Item:...'「部分。作爲參考,這是我的滿級:
import java.util.*;
public class ShoppingCart implements Cart {
private double total;
private double tax;
private double shipping;
private int totalQuantity = 0;
ArrayList<SelectedItem> cartItems = new ArrayList<SelectedItem>();
public ShoppingCart() {
}
public void addItem(SelectedItem newItem) {
for (SelectedItem item: cartItems) {
if (newItem.getItemNumber() == item.getItemNumber()) {
totalQuantity = newItem.getQuantity() + totalQuantity;
} else {
totalQuantity = newItem.getQuantity();
cartItems.add(new SelectedItem(newItem.getItemNumber(), newItem.getDescription(), newItem.getUnitPrice(), newItem.getQuantity()));
}
}
}
public void deleteItem(int deleteItemNumber) {
}
public double getTotal() {
return total;
}
public double getTax() {
tax = total * 0.045;
return tax;
}
public double getShipping() {
if (total <= 10) {
shipping = 2.50;
} else {
shipping = total * 0.15;
}
return shipping;
}
public String toString() {
for (SelectedItem item: cartItems) {
return "Item: " + item.getItemNumber() + "\nQuantity: " + item.getQuantity + "\nUnit Price: $" + item.getUnitPrice + "\nTotal Price: $" + (item.getUnitPrice * item.getQuantity);
}
return "";
}
}
這裏是的SelectedItem類:
public class SelectedItem implements Item {
private int itemNumber;
private String description;
private double unitPrice;
private int quantity;
public SelectedItem(int itemNumber, String description,
double unitPrice, int quantity) {
this.itemNumber = itemNumber;
this.description = description;
this.unitPrice = unitPrice;
this.quantity = quantity;
}
public int getItemNumber() {
return itemNumber;
}
public double getUnitPrice() {
return unitPrice;
}
public String getDescription() {
return description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int newQuantity) {
quantity = newQuantity;
}
}
最後,我的測試類是應該創建SelectedItem對象的實例:
public class CartTester {
public static void main(String[] args) {
ShoppingCart cart1 = new ShoppingCart();
cart1.addItem(new SelectedItem(1, "widget", 0.95, 5));
cart1.addItem(new SelectedItem(2, "gadget", 0.25, 10));
cart1.addItem(new SelectedItem(3, "doohickey", 0.35, 3));
cart1.deleteItem(2);
cart1.addItem(new SelectedItem(3, "doohickey", 0.35, 2));
cart1.addItem(new SelectedItem(4, "thingamabob", 0.12, 7));
System.out.println(cart1.toString());
ShoppingCart cart2 = new ShoppingCart();
for (int i = 1; i <= 1000; i++) {
String nameString = "Volume " + i;
cart2.addItem(new SelectedItem(i, nameString, 10.99, i));
}
System.out.println("**********");
System.out.println(cart2.toString());
}
}
請幫助我!這裏的輸出應該如何看的片段:
Item: widget
Quantity: 5
Unit Price: $0.95
Total Price: $4.75
更新:我已經得到了我的代碼打印出以下幾點:
aItem: widget
Quantity: 5
Unit Price: $0.95
Total Price: $4.75
**********
Item: Volume 1
Quantity: 1
Unit Price: $10.99
Total Price: $10.99
..但它仍然沒有按」打印出ShoppingCart中的所有商品。我修改了我的ShoppingCart類,我認爲問題是AddItem方法或toString方法。這是我更新我的購物類:
import java.util.*;
public class ShoppingCart implements Cart {
private double total;
private double tax;
private double shipping;
private int totalQuantity = 0;
ArrayList<SelectedItem> compareTo = new ArrayList<SelectedItem>();
ArrayList<SelectedItem> cartItems = new ArrayList<SelectedItem>();
String itemDescription = "";
int itemQuantity;
double itemUnitPrice;
double itemTotal;
public ShoppingCart() {
}
public void addItem(SelectedItem newItem) {
boolean found = false;
if (cartItems != null && !cartItems.isEmpty()) {
for (SelectedItem item : cartItems) {
if (newItem.getItemNumber() == item.getItemNumber()) {
found = true;
item.setQuantity(newItem.getQuantity() + item.getQuantity());
}
}
} else {
}
if (found) {
} else {
cartItems.add(new SelectedItem(newItem.getItemNumber(), newItem.getDescription(), newItem.getUnitPrice(), newItem.getQuantity()));
}
}
public void deleteItem(int deleteItemNumber) {
for (SelectedItem item: cartItems) {
if (deleteItemNumber == item.getItemNumber()) {
item.setQuantity(0);
}
}
}
public double getTotal() {
for (SelectedItem item: cartItems) {
total = item.getQuantity() * item.getUnitPrice();
}
return total;
}
public double getTax() {
tax = total * 0.045;
return tax;
}
public double getShipping() {
if (total <= 10) {
shipping = 2.50;
} else {
shipping = total * 0.15;
}
return shipping;
}
public String toString() {
for (SelectedItem item: cartItems) {
return ("Item: " + item.getDescription() + "\nQuantity: " + item.getQuantity() + "\nUnit Price: $"
+ item.getUnitPrice() + "\nTotal Price: $" + (item.getQuantity()*item.getUnitPrice()) + "\n");
}
return "\nTotal: $" + getTotal() + "\nTax: $" + getTax() + "\nShipping: $" + getShipping() +
"\nGRAND TOTAL: $" + (getTotal() + getTax() + getShipping());
}
}
爲什麼要返回for循環? – Steephen
As @Steephen:聲明,循環中的返回使它不再像循環一樣工作。它只是在做你要做的事情。你並沒有進行關鍵的調試步驟 - 你需要在腦海中仔細檢查你的代碼,看看它在運行時的功能,包括在你的腦海中經歷每個循環。如果你沒有學會這樣做,你將無法理解代碼或錯誤。 –
不,我明白,但我需要一個返回語句後for循環,因爲它返回一個錯誤,說:「此方法必須返回字符串類型的結果。」而方法類型是字符串,所以我需要返回一個字符串.. – Dylan