Java 错误:排列边界。如何解决?

Java 错误:排列边界。如何解决?,java,arrays,compiler-errors,indexoutofboundsexception,Java,Arrays,Compiler Errors,Indexoutofboundsexception,我已经编写了这个程序,但编译时遇到了一个逻辑错误。获取index.arrayOutOfBounds 我的输入将是1,2,6,10,用于选择产品,同时输出应为 Total items ordered: 3 Price of items ordered: $747.00 Sales Tax: $48.55 Total amount due: $795.55 我的for循环条件或计算或数组中是否存在导致此输出的错误 import java.util.Scanner; public class Gra

我已经编写了这个程序,但编译时遇到了一个逻辑错误。获取index.arrayOutOfBounds

我的输入将是
1,2,6,10
,用于选择产品,同时输出应为

Total items ordered: 3
Price of items ordered: $747.00
Sales Tax: $48.55
Total amount due: $795.55
我的for循环条件或计算或数组中是否存在导致此输出的错误

import java.util.Scanner;
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};  

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();

    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 terminated by sentinel
    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
    while (naNumber[nCount] != SENTINEL) {

    System.out.println("\nPlease select another item from the menu above: ");

    nCount++;

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

    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: " + 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 ));
    } //End main method

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

  double total = 0;

  //Calculate entered items
  for(int i = 0; i < naNumber.length; i++){
   if(naNumber[i] != 0) {
   total += itemPrices[naNumber[i] - 1];
  }
}

  return total;
  }
} //end class calculateTotalPriceOfItemsOrdered
import java.util.Scanner;
公共类葡萄柚订购阵列{
//声明常量
公共静态最终整数大小=100;
公共静态final int[]itemPrices={492993999199129919995999};
公共静态void main(字符串[]args){
//声明变量
扫描仪输入=新扫描仪(System.in);
字符串客户名称;
int[]naNumber=新的int[SIZE];
int n产品=0;
双总=0;
双dFinalPrice=0.0;
int nCount=0;
//声明常量
最终int哨兵=10;
最终双重销售税=0.065;
//提示用户输入名称
System.out.println(“请输入您的姓名:”);
//输入用户名
CustomerName=input.nextLine();
System.out.println(“”);
//开始关于上面数组的产品列表声明
System.out.println(“葡萄柚产品:”);
System.out.println(“1.gPod shuffle$”+itemPrices[0]);
System.out.println(“2.gPod Touch$”+项目价格[1]);
System.out.println(“3.gPad Mini$”+itemPrices[2]);
System.out.println(“4.gPad 2$”+项目价格[3]);
System.out.println(“5.gPhone$”+项目价格[4]);
System.out.println(“6.gMac$”+项目价格[5]);
System.out.println(“7.MacNovel Pro$”+项目价格[6]);
System.out.println(“8.MacNovel Air$”+项目价格[7]);
System.out.println(“9.MiniMac$”+项目价格[8]);
System.out.println(“10.完成我的订单”);
//继续读取,直到输入被sentinel终止
System.out.println(“\n请从上面的菜单中选择一项:”;
//读取用户输入的数字
naNumber[nCount]=input.nextInt();
//beginwhile循环语句
while(naNumber[nCount]!=哨兵){
System.out.println(“\n请从上面的菜单中选择另一项:”;
nCount++;
//读取用户输入的数字
naNumber[nCount]=input.nextInt();
}  
System.out.println(“感谢您向葡萄柚公司订购,”+客户名称);
//最终价格计算
dFinalPrice=calculateTotalPrice(naNumber、itemPrices、nTotal);
//在屏幕上打印空行
System.out.println(“”);
//订购的产品总数
System.out.println(“订购的总项目:+N计数”);
//订购物品的总价
System.out.println(“订购物品的价格:$”+dFinalPrice);
//与购买相关的销售税
System.out.println(“销售税:$”+销售税*dFinalPrice);
//客户应向葡萄柚公司支付的总金额。
System.out.println(“应付总额:$”+(销售税*dFinalPrice+dFinalPrice));
}//结束主方法
私有静态双重计算TalPrice(int[]naNumber,int[]itemPrices){
双倍合计=0;
//计算输入的项目
for(int i=0;i
您的数组
itemPrices
包含9个元素。因此,当您输入10时,它将使

total += itemPrices[10 - 1];

您的数组在第10位没有任何元素,即itemPrices[9]。因此,ArrayIndexOutOfBoundException将成为一个问题,因为在循环完成后,您的naNumber[nCount]==10然后在
calculateTotalPrice
中尝试访问
total+=itemPrices[9]超出范围

快速解决方案:在您的
while
之后添加
naNumber[nCount]=0


<> P>但是如果用户输入了超过100个产品,你必须考虑你的应用程序会做什么?

考虑使用调试器。堆栈跟踪是什么?任何合适的IDE都会显示异常的行。对我们来说,调试IDE可以立即执行的操作是浪费时间的。在calculateTotalPrice()函数中,for循环将继续循环,直到i小于naNumber.length(naNumber.length=100)。但itemPrice数组只包含9个值。因此,在这之后,它会导致ArrayIndexOutOfBound异常。这工作绝对完美。谢谢你*1000000