Java中使用If语句的数组方法

时间:2016-03-04 05:08:17

标签: java arrays if-statement methods

我是一名学习Java程序的研究生。作为家庭作业,我们要做以下事情:

  • 创建项目菜单,用户可以将选项输入数组
  • 创建一种方法,确定所选项目的价格并返回总和

现在,我的输出显示从方法返回的总数实际上是在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周,而且我已经绝望了。我已经阅读了本网站上与此主题相关的大多数文章,分别阅读了许多关于方法和数组的文章,但仍然无法制定工作程序。任何帮助都将受到赞赏。

2 个答案:

答案 0 :(得分: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数组。根据您的需要修改我的代码。它应该更好地概述代码。并且消除了意外收取客户价格的风险,而不是宣布的价格。

答案 1 :(得分: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;
}