【问题标题】:How do I write a method to calculate total cost for all items in an array?如何编写一种方法来计算数组中所有项目的总成本?
【发布时间】:2011-03-05 04:02:24
【问题描述】:

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

主文件

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  );                              
}

}

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

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

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

    total 应该限定范围,以便可以从 main 中访问它,除非您想在函数调用之间传递它。然后,您可以打印出总数,也可以创建一个为您打印出来的方法。

    【讨论】:

      【解决方案2】:

      数组中的一共7个数字可以创建为:

      import java.util.*;
      class Sum
      {
         public static void main(String arg[])
         {
           int a[]=new int[7];
           int total=0;
           Scanner n=new Scanner(System.in);
           System.out.println("Enter the no. for total");
      
           for(int i=0;i<=6;i++) 
           {
             a[i]=n.nextInt();
             total=total+a[i];
            }
             System.out.println("The total is :"+total);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        • 1970-01-01
        • 1970-01-01
        • 2015-02-04
        • 2015-09-09
        • 1970-01-01
        相关资源
        最近更新 更多