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

发布于 2024-10-20 10:07:12 字数 3438 浏览 2 评论 0原文

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

主文件

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

徒留西风 2024-10-27 10:07:12

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

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

Total 应该有作用域,因此可以从 main 内部访问它,除非您想在函数调用之间传递它。然后,您可以只打印总数,也可以创建一个为您打印总数的方法。

In your for loop you need to multiply the units * price. 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. 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. Then you can either just print out the total or create a method that prints it out for you.

友谊不毕业 2024-10-27 10:07:12

数组中总共 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);
    }
}

The total of 7 numbers in an array can be created as:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文