我很难弄清楚为什么这段代码在“TOTAL COST”中最终没有加起来。
两个班级:
ShoppingCartPrinter.java和ItemToPurchase.java
我一直得到的结果是“TOTAL COST”全部为0。任何帮助将不胜感激。感谢。
ShoppingCartPrinter.java代码:
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine(); // Sets the variable productName for user input
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt(); // Set the variable productPrice for user input
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt(); // Set the variable productQuantity for user user input
System.out.println("");
item1.setName(productName);
item1.setPrice(productPrice);
item1.setQuantity(productQuantity);
System.out.println("Item 2");
System.out.println("Enter the item name: ");
scnr.nextLine();
productName = scnr.nextLine(); // Set the variable productName for user input
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt(); // Set the variable productPrice for user input
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt(); // Set the variable productQuantity for user input
System.out.println("");
item2.setName(productName);
item2.setPrice(productPrice);
item2.setQuantity(productQuantity);
cartTotal = (item1.getPrice() * item1.getQuantity()) + (item2.getPrice() * item2.getQuantity());
System.out.println("TOTAL COST");
System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + (item1.getPrice() * item1.getQuantity()));
System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + (item2.getPrice() * item2.getQuantity()));
System.out.println("");
System.out.println("Total: $" + cartTotal);
return;
}
}
ItemToPurchase.java
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
return;
}
public void setName(String name) {
itemName = name;
return;
}
public void setPrice(int price) {
itemPrice = 0;
return;
}
public void setQuantity (int quantity) {
itemQuantity = 0;
return;
}
public String getName() {
return itemName;
}
public int getPrice() {
return itemPrice;
}
public int getQuantity() {
return itemQuantity;
}
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice + " = $" + (itemPrice * itemQuantity));
}
}
答案 0 :(得分:1)
问题出在setters
。您正在为项目分配0(零)。
itemPrice = 0;
和
itemQuantity = 0;
这可以通过以下方式解决:
itemPrice = price;
和
itemQuantity = quantity;
此外,在每个具有return
返回类型的方法的末尾,您不需要void
语句,尤其是在ItemToPurchase()
构造函数中。你可以安全地删除它们。
答案 1 :(得分:1)
zybooks 赋值会自动应用 setter,这不是自愿的。只有某些部分的代码可以编辑。我目前正在研究这个项目,并且也遇到了问题。只是想澄清这不是他们设置为 0 的编码器错误