简体   繁体   English

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

[英]How do I write a method to calculate total cost for all items in an array?

I'm stuck. 我被卡住了。 This is what I have written so far, but I don't know how to set up for a method call to prompt for the total. 到目前为止,这是我写的内容,但是我不知道如何设置方法调用以提示总数。 I need the individual totals for all items in the array to be added to get a total cost and it needs to be displayed at the end of the program. 我需要将数组中所有项目的总计加起来以获得总成本,并且需要在程序末尾显示它。 Please, any advice is helpful. 请,任何建议都是有帮助的。 I have to be to work soon and need to turn it in before I go. 我必须尽快工作,并且需要在上班之前将其上交。 Thanks 谢谢

MAIN FILE 主文件

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

2ND CLASS 二等班

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

} }

In your for loop you need to multiply the units * price. 在for循环中,您需要乘以单位*价格。 That gives you the total for that particular item. 这样就为您提供了该特定项目的总计。 Also in the for loop you should add that to a counter that keeps track of the grand total. 同样在for循环中,您应该将其添加到跟踪总计的计数器中。 Your code would look something like 您的代码看起来像

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

total should be scoped so it's accessible from within main unless you want to pass it around between function calls. total应该是作用域的,以便可以从main内部访问它,除非您希望在函数调用之间传递它。 Then you can either just print out the total or create a method that prints it out for you. 然后,您可以只打印总计,也可以创建一个为您打印出来的方法。

The total of 7 numbers in an array can be created as: 数组中总共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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何计算用户从5个组合框中选择的项目的总成本 - How do i calculate the total cost of items the user selects from 5 comboboxs 我如何计算所有订单总数? - how do i calculate all the total orders together? 如何将ArrayList对象中所有值的总成本相加? - How do I add up the total cost for all values from the objects within ArrayList? 如何获取 for 循环中所有项目的总金额? - How do I get the total amount on all items on a for loop? 如何使用一种方法来计算1个等级的数组参数并返回数组的总数? - How do you use a method to calculate 1 array parameter of grades and returns the total of the array? 如何在java中打印数组中所有数字的总数或总和? - How do I print the total or sum of all the digits in an array in java? 如何计算 64 位 Java 内存成本 - How do I calculate 64bit Java Memory Cost 如何从文件中汇总服务和服务总成本? - How do I total up services and total cost of services from a file? 如果每个“交易”都是一个对象,那么如何存储所有交易的总成本? - If each 'transaction' is an object, how do you store the total cost of all transactions made? 如何从Java数组中提取项目,然后放入另一个数组中,并计算总计 - How to extract items out of an array in Java, and place into another array, and calculate a total
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM