2011-03-05 33 views
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 );        
} 

}

回答

1

在你的循環,你需要乘以單位*價格。這給你這個特定項目的總數。同樣在for循環中,您應該將其添加到跟蹤總計的計數器。你的代碼看起來像

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

總應該是有限的,所以它可以從主內部訪問,除非你想在函數調用之間傳遞它。然後,您可以打印總數或創建一個方法將其打印出來。

0

在陣列中的總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); 
    } 
} 
相關問題