gpt4 book ai didi

java - 编译但未计算总计

转载 作者:行者123 更新时间:2023-12-02 02:44:48 25 4
gpt4 key购买 nike

我得到的不是计算输出,而是“null”,而没有我正在寻找的实际总打印输出。我认为这是因为 Item 没有正确定义。忽略shop.txt,它对我的​​问题没有影响

package Shopping;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
public class Shop
{
public static void main(String[] args)
{
//I/O stream

String fileName = "shop.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName +
"\ncontains the following lines:\n");
try
{
inputStream = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("This is a shopping list " +
fileName);
System.exit(0);
}
while (inputStream.hasNextLine())
{
String line = inputStream.nextLine();
System.out.println(line);
}
inputStream.close();


ArrayList<Item> Cart = new ArrayList<Item>();

Item item;
String itemName;
double itemPrice;
int quantity;
double totalPrice = 0.0;
double sum = 0.0;
int priority;

Scanner scan = new Scanner(System.in);
String continueShopping = "y";
do
{
System.out.print("Enter the name of the item: ");
itemName = scan.nextLine();
System.out.print("Enter the unit price: ");
itemPrice = scan.nextDouble();
System.out.print("Enter the quantity: ");
quantity = scan.nextInt();


// create a new item and add it to the cart

item = new Item(itemName, itemPrice, quantity);
Cart.add(item);

for (int i = 0; i < Cart.size(); i++)
{
Item itm = Cart.get(i);
System.out.println(itm);
}
// Print out the results

System.out.print("Continue shopping (y/n)? ");
scan.nextLine();
continueShopping = scan.nextLine();
}
while (continueShopping.equals("y"));
for (int i = 0; i < Cart.size(); i++)
{
Item itm = Cart.get(i);
System.out.println(itm);
totalPrice = itm.getQuantity() * itm.getPrice();
sum += totalPrice;
}
NumberFormat type = NumberFormat.getCurrencyInstance();
System.out.println("The total price is: " + type.format(sum));
}
}

元素类别

package Shopping;
import java.text.NumberFormat;

public class Item
{

private String name;
private double price;
private int quantity;


public Item(String itemName, double itemPrice, int quantity2)
{
}

public void Item(String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}

//Info about the item

public String toString()
{
NumberFormat type = NumberFormat.getCurrencyInstance();

return (name + "\t" + type.format(price) + "\t" + quantity + "\t"
+ type.format(price * quantity));
}

//Retrieve the item price

public double getPrice()
{
return price;
}

//Retrieve item name

public String getName()
{
return name;
}

//Retrieve quantity

public int getQuantity()
{
return quantity;
}
}

最佳答案

您的 Item 类有一个空构造函数

public Item(String itemName, double itemPrice, int quantity2) {
}

和一个名为 Item

的方法
public void Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}

删除空构造函数并从方法中删除返回类型 void 以将其转换为构造函数。

关于java - 编译但未计算总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44792370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com