2016-03-04 102 views
0

我是一名研究生,學習Java程序。作爲一個家庭作業,我們要做到以下幾點:使用Java中的If語句的數組方法

  • 創建的菜單項,用戶可以輸入選擇到一個數組
  • 創建計算出一個方法的項目的價格選擇,並返回總和

現在,我的輸出顯示,從方法返回的總實際上是將所有的價格在if語句,而不是與被複制的用戶輸入陣列協調人(見下方示例輸出) 。我們還沒有涉及比標準數組更多的東西(沒有ArrayList或其他方法)。請提供任何建議,我會盡我所能找出答案。

import java.util.Arrays; 
import java.util.Scanner; 



public class Online_Purchasing_HW6 { 

public static final int ARRAY_LENGTH = 10;  //Length of the array 


public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); //Reads user input 
    int[] naMenuChoice = new int [ARRAY_LENGTH]; //Array for items chosen 
    String[] naMenu = new String [ARRAY_LENGTH]; //Array for menu items 
    int[] naItemPrice = new int [9]; //Array for item prices 
    String sCustomerName;   //String for customer's name 
    int nChoice = 0;    //Option chosen in menu by user/Used as Index 
    int nSum = 0;    //Sum of items chosen 
    double dTotalPrice = 0;  //Total price plus taxes 
    double dTaxes = 0;   //Total taxes to be applied to total 

    //Declare Constants 
    final int SENTINEL = 10;    //Used to end loop 
    final double SALES_TAX = 0.065;   //Sales tax to be used 


    //Choices for menu denoted by strings 
    String sChoice1 = "1. Smartphone" + "\t" + "\t" + "$249"; 
    String sChoice2 = "2. Smartphone Case" + "\t" + "$39"; 
    String sChoice3 = "3. PC Laptop" + "\t" + "\t" + "$1149"; 
    String sChoice4 = "4. Tablet" + "\t" + "\t" + "$349"; 
    String sChoice5 = "5. Tablet Case" + "\t" + "\t" + "$49"; 
    String sChoice6 = "6. eReader" + "\t" + "\t" + "$119"; 
    String sChoice7 = "7. PC Desktop" + "\t" + "\t" + "$899"; 
    String sChoice8 = "8. LCD Monitor" + "\t" + "\t" + "$299"; 
    String sChoice9 = "9. Laser Printer" + "\t" + "$399"; 
    String sChoice10 = "10. Complete my order"; 




    //Prompt user for name 
    System.out.print("Please enter your name: "); 

    //Read customer's name 
    sCustomerName = input.nextLine(); 


    //Menu of items for purchase 
    System.out.print("\n"); 
    System.out.println("Best Purchase Products"); 
    System.out.println(sChoice1); 
    System.out.println(sChoice2); 
    System.out.println(sChoice3); 
    System.out.println(sChoice4); 
    System.out.println(sChoice5); 
    System.out.println(sChoice6); 
    System.out.println(sChoice7); 
    System.out.println(sChoice8); 
    System.out.println(sChoice9); 
    System.out.println(sChoice10); 

    //Prompt user for item selection 
    System.out.print("Please select an item from the menu above: "); 


    naMenuChoice[nChoice] = input.nextInt(); 


     //Loop to read integers from user 
     while (naMenuChoice[nChoice] != SENTINEL) {   

     //adds 1 everytime more than one item is chosen by the user 
     nChoice++;  

     //Prompt user for another choice since he/she has not chosen option "10" 
     System.out.print("Please select another item from the menu above: "); 
     naMenuChoice[nChoice] = input.nextInt(); 

     } //end of while loop 

    System.out.print(Arrays.toString(naMenuChoice)); 
    //If option 10 if chosen, the loop will end with this message 
    System.out.println("\n" + "Thank you for ordering with Best Purchase, " + sCustomerName); 

    //call calculateTotalPrice method passing Array    
    nSum = nCalculatePrice(naMenuChoice); 

    //Formulas 

    //Sales Tax 
    dTaxes = SALES_TAX * nSum; 

    //Total Amount Due 
    dTotalPrice = dTaxes + nSum; 


    System.out.println("Total items ordered: " + nChoice); 
    System.out.println("Price of items ordered: $" + nSum); 
    System.out.println("Sales tax: $" + dTaxes); 
    System.out.println("Total amount due: $" + dTotalPrice); 
}//end of main method 


//Method for calculating price 
public static int nCalculatePrice(int[] naChoicesToPrice){ 
int nTotalPrice = 0; //int value to return 
int nIndex = 0; //used as counter 
int nItemPrice = 0; //used to assign price of items 
int[] naAddedPrices = new int[ARRAY_LENGTH]; //new array for assigning prices 

//For loop to sum up all entries from naItemPrice array 
for (nIndex = 0; nIndex < ARRAY_LENGTH; nIndex++){ 
    naAddedPrices[nIndex] = naChoicesToPrice[nIndex]; 
//end of For Loop 

    if (nIndex == 1) { 
     nItemPrice = 249; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 2) { 
     nItemPrice = 39; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 3) { 
     nItemPrice = 1149; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 4) { 
     nItemPrice = 349; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 5) { 
     nItemPrice = 49; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 6) { 
     nItemPrice = 119; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 7) { 
     nItemPrice = 899; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 8) { 
     nItemPrice = 299; 
     nTotalPrice += nItemPrice;} 
    if (nIndex == 9) { 
     nItemPrice = 399; 
     nTotalPrice += nItemPrice;} 
} //end of for loop 

return nTotalPrice; 
}//end of naCalculatePrice method 

}//end of class 

該程序對應的輸出是:

Please enter your name: John Smith 

    Best Purchase Products 
1. Smartphone  $249 
2. Smartphone Case $39 
3. PC Laptop  $1149 
4. Tablet  $349 
5. Tablet Case  $49 
6. eReader  $119 
7. PC Desktop  $899 
8. LCD Monitor  $299 
9. Laser Printer $399 
10. Complete my order 
Please select an item from the menu above: 9 
Please select another item from the menu above: 10 
[9, 10, 0, 0, 0, 0, 0, 0, 0, 0] 
Thank you for ordering with Best Purchase, John Smith 
Total items ordered: 1 
Price of items ordered: $3551 
Sales tax: $230.815 
Total amount due: $3781.815 

正如你所看到的,我從菜單中選擇只有一個項目,但它增加了所有的價格。到目前爲止,我一直在做這項工作2周,而且我越來越絕望。我已閱讀了本網站上關於此主題的大部分文章,分別閱讀了許多關於方法和數組的文章,並且仍然無法制定一個工作程序。任何幫助將不勝感激。

+0

是什麼樣的智能手機的情況呢? 39美元非常陡峭。請查看http://stackoverflow.com/help/how-to-ask。家庭作業問題通常會被關閉和降低。 – Mathemats

+0

'naAddedPrices'服務的目的是什麼? –

+0

我不確定是否必須初始化一個新數組,以便在使用方法時可以將原始數組複製到該數組中。所以,我創建了naAddedPrices –

回答

0

到目前爲止工作出色。

在您的for循環中總結一下,您必須查看naMenuChoices以查看客戶訂購的內容。請記住,您可能會在那裏找到0或10,在這種情況下,您不應該添加任何內容。祝你好運,你離目標不遠。

你可能想使自己的產品一類/項目,如

public class Product { 
    private String itemText; 
    private int itemPrice; 

    public Product(String itemText, int itemPrice) { 
     this.itemText = itemText; 
     this.itemPrice = itemPrice; 
    } 

    public int getItemPrice() { 
     return itemPrice; 
    } 

    @Override 
    public String toString() { 
     return itemText + "\t\t$" + itemPrice; 
    } 
} 

填充9個物體該類到一個數組,用它們來構建sChoice字符串,並用它來代替naItemPrice陣列。修改我的代碼以滿足您的需求。它應該給出更好的代碼概述。並消除意外收取客戶另一價格的風險。

0
for (int i = 0; i < ARRAY_LENGTH; i++){ 
if(naChoicesToPrice[i]==0 || naChoicesToPrice[i]==10){ 
    nItemPrice = 0; 
} 
else{ 
    if (naChoicesToPrice[i] == 1) { 
    nItemPrice = 249; 
    } 
    if (naChoicesToPrice[i] == 2) { 
     nItemPrice = 39; 
    } 
    if (naChoicesToPrice[i] == 3) { 
     nItemPrice = 1149; 
    } 
    if (naChoicesToPrice[i] == 4) { 
     nItemPrice = 349; 
    } 
    if (naChoicesToPrice[i] == 5) { 
     nItemPrice = 49; 
    } 
    if (naChoicesToPrice[i] == 6) { 
     nItemPrice = 119; 
    } 
    if (naChoicesToPrice[i] == 7) { 
     nItemPrice = 899; 
    } 
    if (naChoicesToPrice[i] == 8) { 
     nItemPrice = 299;    
    } 
    if (naChoicesToPrice[i] == 9) { 
     nItemPrice = 399;   
    } 
}  
nTotalPrice += nItemPrice; 
} //end of for loop 
return nTotalPrice; 
} 
+0

您不需要爲每個if語句使用nTotalPrice求和,否則在循環外部使用它一次。我更喜歡用這種例子的開關盒。另一個建議是爲項目名稱和itemprices使用getter和setter。請讓我知道我的建議是否有幫助。 –