【问题标题】:Array out of bounds>数组越界>
【发布时间】:2020-01-24 06:32:24
【问题描述】:

我需要创建一个程序来计算您将收到的更改,但它说:

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:索引 长度为 5 的 9 出界 在 Main.main(Main.java:34)

我对在 Java 中使用数组还很陌生,所以任何指针都会有所帮助

import java.math.*;

public class Main {

    static int changeTotal, penny, nickel, dime, quarter, dollarBill,  fiveBill, tenBill, twentyBill, fiftyBill, hundredBill;
    static double payed, price;

    public static void main(String args[]) {

        Scanner myScan = new Scanner(System.in);

        System.out.println("Enter cost of item");
        price = myScan.nextDouble();
        System.out.println("Enter money given");
        payed = myScan.nextDouble();

        int priceInt = (int) (price * 100);
        int payedInt = (int) (payed * 100);

        penny = 1;
        nickel = 5;
        dime = 10;
        quarter = 25;
        dollarBill = 100;

        int[] currencyArray = {penny, nickel, dime, quarter, dollarBill};
        int[] changeCount = {0, 0, 0, 0, 0}; 
        if(payed > price) {
        changeTotal = payedInt - priceInt;
        }

        for(int i = 9; i >= 0; i--) {
          int remainder = (changeTotal % currencyArray[i]);
            if(remainder < changeTotal) { 
                changeCount[i] = (changeTotal - remainder) / currencyArray[i]; 
            changeTotal = remainder;
            }
        }

        System.out.println(payed - price);
        System.out.println("Ones: " + changeCount[4]);
        System.out.println("Quarters: " + changeCount[3]);
        System.out.println("Dimes: " + changeCount[2]);
        System.out.println("Nickels: " + changeCount[1]);
        System.out.println("Pennies: " + changeCount[0]);
    }
}

【问题讨论】:

  • 问题出在你的for(int i = 9; i &gt;= 0; i--)currencyArraychangeCount 只有 5 个元素,您尝试迭代 9 个元素。短期修复将您的 for 的限制更改为 i = 4

标签: java


【解决方案1】:

要补充以前的答案,我建议使用对数组长度的引用,而不是硬连接值。处理这样的情况可能是一种不错的做法,因为从那时起,如果数组将来由于某种原因发生更改,您就不必追踪硬编码的值调整它。比如for(int i = currencyArray.length - 1; i &gt;= 0; i--)

【讨论】:

    【解决方案2】:
    int[] currencyArray = {penny, nickel, dime, quarter, dollarBill};
    

    这会在您的数组中分配 5 个元素,现在您的数组大小为 5,这意味着您可以访问索引 0 到 4。

    for(int i = 9; i >= 0; i--) {
              int remainder = (changeTotal % currencyArray[i]);
    

    在这一行中,您尝试索引未分配给数组的 9。因此它是数组超出范围的索引。

    【讨论】:

      【解决方案3】:

      currencyArray 的长度为 5,在您的代码中,您尝试访问不存在的索引 9 的元素,这就是您收到此异常的原因。

      从最后一个索引开始编辑你的循环

      for(int i = currencyArray.length-1; i >= 0; i--) {
        int remainder = (changeTotal % currencyArray[i]);
        if(remainder < changeTotal) { 
            changeCount[i] = (changeTotal - remainder) / currencyArray[i]; 
            changeTotal = remainder;
        }
      }
      

      currencyArray.length返回数组的长度,因为字符串索引为0,所以需要从长度中减去1,从最后一个索引开始

      【讨论】:

        猜你喜欢
        • 2012-10-28
        • 2019-05-03
        • 2011-06-23
        • 1970-01-01
        • 2023-03-17
        • 2019-05-10
        • 2014-11-16
        • 2012-11-04
        • 2013-07-27
        相关资源
        最近更新 更多