我是一名研究生,學習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周,而且我越來越絕望。我已閱讀了本網站上關於此主題的大部分文章,分別閱讀了許多關於方法和數組的文章,並且仍然無法制定一個工作程序。任何幫助將不勝感激。
是什麼樣的智能手機的情況呢? 39美元非常陡峭。請查看http://stackoverflow.com/help/how-to-ask。家庭作業問題通常會被關閉和降低。 – Mathemats
'naAddedPrices'服務的目的是什麼? –
我不確定是否必須初始化一個新數組,以便在使用方法時可以將原始數組複製到該數組中。所以,我創建了naAddedPrices –