-1

次のプログラムを作成しましたが、「calculateTotalPrice」メソッドをメイン メソッドに呼び出して最終価格を計算しようとすると問題が発生します。メソッドまたは呼び出しのエラーかどうかはわかりません。Main は、「calculateTotalPrice」メソッドを呼び出さなくても、必要に応じて機能します。セカンダリ メソッドまたは call ステートメントをどのように再配置する必要があるかについて、誰かが何か提案をすることはできますか? ありがとう

public class GrapefruitOrderingArray {

//Declare Constants 
public static final int SIZE = 100;


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declare Variables
Scanner input = new Scanner (System.in);     //Input new scanner system
String CustomerName;                         //Declare customer's name as a string 
int nNumber = 0;                             //Declare integer variable for nNumber
int nProducts = 0;                           //Declare integer variable for nproducts
int nTotal;                                  //Declare integer variable for ntotal
double[] nFinalPrice = new double [SIZE];
int [] itemPrices = {49,299,329,399,199,1299,1199,999,599};      //Declare integer variable array
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 10
System.out.println("\nPlease select an item from the menu above: ");

//Begin while-loop statement
while (nNumber != SENTINEL) {
 //Read number entered by the user
 nNumber = input.nextInt();

 if (nNumber == SENTINEL) {   
 System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);

 break;
} 
//Call final price calculation
nFinalPrice = calculateTotalPrice(nNumber); 

//If desired the user may select another or multiple products now    
System.out.println("\nPlease select another item from the menu above: ");
}  
    //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 itemPrice     Individualized product prices
* @param nProduct          Total price of items paid for
* @return nTotals      Returns the number of the product associated with it's initialized price
*/
private static double[] calculateTotalPrice(int nNumber) {

//Calculate entered items
nTotal = nTotal + itemPrices [nNumber-1];

//Increment the total number of products entered    
 nProducts++;

 return nNumber;

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

2 に答える 2

0

このプログラムは、顧客が選択したアイテムの合計価格を返します。それがあなたを助けることを願っています。

import java.util.*;
public class CalTotalPrice {
    static int totalPrice=0;
    static int [] itemPrices = {49,299,329,399,199,1299,1199,999,599};  

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);     

        //declaring variables
        String CustomerName;                                                        
        int itemNumber = 0;                                                  
        int noOfProducts = 0;


        final int SENTINEL = 10;
        final double SALES_TAX = 0.065;

        //Getting customer name
        System.out.println("Please enter your name: ");
        CustomerName = input.nextLine();
        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");
        System.out.print("\nPlease select an item from the menu above: ");

        while (itemNumber != SENTINEL) {
            itemNumber = input.nextInt();

            if (itemNumber <= SENTINEL) {   
                System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);
                break;
            } 

            noOfProducts++; //increse number of products ordered
            totalPrice= calculateTotalPrice(itemNumber);    
            System.out.print("\nPlease select another item from the menu above: ");
        }  

                System.out.println("");
                System.out.println("Total items ordered: " + noOfProducts);
                System.out.println("Price of items ordered: $" +totalPrice);
                System.out.println("Sales tax: $" + SALES_TAX *totalPrice);
                System.out.println("Total amount due: $" + (SALES_TAX *totalPrice+totalPrice));

    }


    public static int calculateTotalPrice(int itemNumber) {

        totalPrice=totalPrice+ itemPrices[itemNumber-1];
        return totalPrice;

    }

}
于 2013-10-18T19:32:23.453 に答える
0

ここにはいくつかの問題があります。

  • int nTotal =0;int nTotalがパラメーターとして取り込まれた 後に発生します。
  • SIZEどこにも宣言されていません。
  • 余談ですが、あなたのコメントは過度に過剰です。
于 2013-10-18T17:29:42.850 に答える