Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写一个方法来计算数组中所有项目的总成本?_Java_Arrays_Methods - Fatal编程技术网

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

Java 如何编写一个方法来计算数组中所有项目的总成本?,java,arrays,methods,Java,Arrays,Methods,我卡住了。这是我到目前为止写的,但我不知道如何设置方法调用来提示总计。我需要添加数组中所有项目的单独总计,以获得总成本,并且需要在程序结束时显示。请,任何建议都是有用的。我很快就要上班了,在我走之前必须把它交上来。谢谢 主文件 package inventory2; import java.util.Scanner; public class RunApp { public static void main(String[] args) { Scann

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

主文件

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

}

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

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

total应该是作用域,这样就可以从main中访问它,除非您想在函数调用之间传递它。然后,您可以打印出总数,也可以创建一个方法来打印出来。

一个数组中的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);
    }
}
import java.util.*;
班级总数
{
公共静态void main(字符串arg[])
{
int a[]=新int[7];
int-total=0;
扫描器n=新扫描器(System.in);
System.out.println(“输入总数的编号”);
对于(int i=0;i
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);
    }
}