gpt4 book ai didi

java - 如何编写一个方法来计算数组中所有项目的总成本?

转载 作者:行者123 更新时间:2023-11-29 06:15:38 25 4
gpt4 key购买 nike

我卡住了。这是我到目前为止所写的,但我不知道如何设置方法调用来提示总数。我需要添加数组中所有项目的单独总计以获得总成本,并且需要在程序结束时显示。拜托,任何建议都是有帮助的。我很快就要上类了,去之前需要把它上交。谢谢

主文件

package inventory2;
import java.util.Scanner;

public class RunApp
{
public static void main(String[] args)
{

Scanner input = new Scanner( System.in );

Items theItem = new Items();

int number;
String Name = "";

System.out.print("How many items are to be put into inventory count?: ");
number = input.nextInt();
input.nextLine();

Items[]inv = new Items[number];


for(int count = 0; count < inv.length; ++count)
{
System.out.print("\nWhat is item " +(count +1) + "'s name?: ");
Name = input.nextLine();
theItem.setName(Name);

System.out.print("Enter " + Name + "'s product number: ");
double pNumber = input.nextDouble();
theItem.setpNumber(pNumber);

System.out.print("How many " + Name + "s are there in inventory?: ");
double Units = input.nextDouble();
theItem.setUnits(Units);

System.out.print(Name + "'s cost: ");
double Price = input.nextDouble();
theItem.setPrice (Price);

inv[count] = new Items(Name, Price, Units, pNumber);
input.nextLine();

System.out.print("\n Product Name: " + theItem.getName());
System.out.print("\n Product Number: " + theItem.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theItem.getUnits());
System.out.print("\n Price per Unit: " + theItem.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f", theItem.getName(), theItem.calculateTotalPrice());
System.out.printf("Total Cost for all items entered: $%.2f", theItem.calculateTotalPrice()); //i need to prompt for output to show total price for all items in array
}
}
}

二等舱

package inventory2;

public class Items
{
private String Name;
private double pNumber, Units, Price;

public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}

//constructor
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
Name = productName;
pNumber = productNumber;
Units = unitsInStock;
Price = unitPrice;
}
//setter methods
public void setName(String n)
{
Name = n;
}

public void setpNumber(double no)
{
pNumber = no;
}

public void setUnits(double u)
{
Units = u;
}

public void setPrice(double p)
{
Price = p;
}

//getter methods
public String getName()
{
return Name;
}

public double getpNumber()
{
return pNumber;
}

public double getUnits()
{
return Units;
}

public double getPrice()
{
return Price;
}

public double calculateTotalPrice()
{
return (Units * Price);
}

public double calculateAllItemsTotalPrice() //i need method to calculate total cost for all items in array
{
return (TotalPrice );
}

最佳答案

在您的 for 循环中,您需要乘以单位 * 价格。这为您提供了该特定项目的总数。同样在 for 循环中,您应该将其添加到跟踪总计的计数器中。您的代码看起来像

float total;
total += theItem.getUnits() * theItem.getPrice();

total 应该有范围,这样它就可以从 main 中访问,除非你想在函数调用之间传递它。然后您可以只打印出总数或创建一个方法来为您打印出来。

关于java - 如何编写一个方法来计算数组中所有项目的总成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201576/

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