以备用方法发出编译变量以输出所需的语句?

时间:2013-10-21 22:14:45

标签: java

我编写了这个程序,我的方法调用“calculateTotalPrice”出现问题。我有一个逻辑错误,没有编译我的代码到正确的输出格式(给出最终价格,订购的项目,与购买相关的税收和总价格)。如果有人可以尝试调整我的代码以允许程序正确编译,那将非常感谢,谢谢。

public class GrapefruitOrderingArray {

//Declare Constants 
public static final int SIZE = 100;
public static final int[] itemPrices = {49,299,329,399,199,1299,1199,999,599};  //Declare integer variable array itemPrices


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
 // Declare Variables
    Scanner input = new Scanner (System.in);                                    
    String CustomerName;                                                        
    int[] naNumber = new int [SIZE];                                            
    int nProducts = 0;                                                          
    double nTotal = 0;                                                          
    double dFinalPrice = 0.0;                                                   
    int nCount = 0;                                                             


    //Declare Constants 
    final int SENTINEL = 10; 
    final double SALES_TAX = 0.065;

    //Prompt user to enter name
    System.out.println("Please enter your name: ");

    //Enter user name
    CustomerName = input.nextLine();

    //Print Blank Line 
    System.out.println("");

    //Begin Product Listing Declarations with respect to array above
    System.out.println("GRAPEFRUIT PRODUCT:");

    System.out.println("1. gPod shuffle $" + itemPrices[0]);

    System.out.println("2. gPod Touch   $" + itemPrices[1]);

    System.out.println("3. gPad Mini    $" + itemPrices[2]);

    System.out.println("4. gPad 2       $" + itemPrices[3]);

    System.out.println("5. gPhone       $" + itemPrices[4]);

    System.out.println("6. gMac         $" + itemPrices[5]);

    System.out.println("7. MacNovel Pro $" + itemPrices[6]);

    System.out.println("8. MacNovel Air $" + itemPrices[7]);

    System.out.println("9. MiniMac      $" + itemPrices[8]);

    System.out.println("10. Complete my order");

    //Keep reading until the input is 100
    System.out.println("\nPlease select an item from the menu above: ");

    //Read number entered by the user
    naNumber[nCount] = input.nextInt();



     //Begin while-loop statement
    if (naNumber[nCount] != SENTINEL)   
 {
    //If desired the user may select another or multiple products now    
    System.out.println("\nPlease select another item from the menu above: ");

    //Read number entered by the user
    naNumber[nCount] = input.nextInt();

    nCount++;
 }

    System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);



        //Call final price calculation
        dFinalPrice = calculateTotalPrice(naNumber,itemPrices,nTotal);


            //Print blank line to screen
            System.out.println("");

            //Total amount of product ordered
            System.out.println("Total items ordered: " + nProducts );

            //Total price of items ordered
            System.out.println("Price of items ordered: $" + nTotal );

            //Sales tax associated with the purchase
            System.out.println("Sales tax: $" + SALES_TAX * nTotal);

            //Total amount due by the customer to Grapefruit Co. 
            System.out.println("Total amount due: $" + (SALES_TAX * nTotal + nTotal));
    } //End main method

/**
 * This method calculates the total price of the products ordered
 * @param naNumber      Individualized product prices
 * @param itemPrices          Total price of items paid for
 * @return nTotal       Returns the number of the product associated with it's initialized  price
 */
  private static double calculateTotalPrice(int[] naNumber, int[] itemPrices, double nTotal) {

    int nCount = 0;
    int nProducts = 0;

    //Calculate entered items
    for(int i = 0; i < naNumber.length; i++){
      if(naNumber[i] != 0) {
      double itemTotal = itemPrices[i] * naNumber[i];
      nTotal += itemTotal;
      }
    }

    return nTotal;




  } //end method calculateTotalPriceOfItemsOrdered 
} //end class calculateTotalPriceOfItemsOrdered

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但我相信问题就在这里:

nTotal = nTotal + itemPrices[naNumber[nCount]-1];

这不会计算每件商品的总价格。

假设int[] naNumber是您购买商品的次数而int[] itemPrices是相应价格的数组,则应将其更改为以下内容:

for(int i = 0; i < naNumber.length; i++){
 if(naNumber[i] != 0) {
  double itemTotal = itemPrices[i] * naNumber[i];
  nTotal += itemTotal;
 }
}

其他一些评论:

  • 您的评论错误或不需要。示例:
  

/ **    * @param args命令行参数    * /

很明显,这是唯一可能的论点。你必须描述它们代表的,而不是它们是什么。

  

//读取用户输入的数字

什么号码?

  

//开始while循环语句

没有while循环。

等等。

  • private static double calculateTotalPrice(int[] naNumber, int[] itemPrices, double nTotal)

您不必传递double nTotal,它没有用处。

修复输出:

        //Total amount of product ordered
        System.out.println("Total items ordered: " + nCount);

        //Total price of items ordered
        System.out.println("Price of items ordered: $" + dFinalPrice);

        //Sales tax associated with the purchase
        System.out.println("Sales tax: $" + SALES_TAX * dFinalPrice );

        //Total amount due by the customer to Grapefruit Co. 
        System.out.println("Total amount due: $" + (SALES_TAX * dFinalPrice + dFinalPrice ));