有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java数组索引超出范围?

有人能帮我解决下面发布的一个错误吗?程序正在运行,但在调用显示捐赠时,我收到了一个有关主屏幕的错误。我如何修复它,使其运行?这与语法错误有关吗?这是我第一次使用数组,所以我欢迎大家反馈我的好习惯。谢谢

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
        at donations.processDonations(donations.java:78)
        at donations.main(donations.java:33)

    import java.util.Scanner; //needed for input

    public class donations {
        static double[] cashDonations = new double[6]; // stores the cash donations
                                                        // from 6 sites
        static double[] lbsFood = new double[6]; // stores the pounds of food
                                                    // donated from 6 sites
        static String[] siteName = new String[6]; // stores the names of the 6 sites
        static String bestSiteCash = " "; // stores the site name with the highest
                                            // cash donation
        static String bestSiteFood = " "; // stores the site name with the highest
                                            // food donation
        static double totalCash = 0; // stores the total cash collected for all
                                        // sites
        static double totalFood = 0; // stores the total food pounds collected for
                                        // all sites
        static double maxFood = 0; // store the highest food collection
        static double maxCash = 0; // stores the highest cash collection

        public static void main(String[] args) {

            Scanner input = new Scanner(System.in); // needed for input
            String anotherCourse = "yes"; // variable to control running program
                                            // again
            do {

                getDonations();
                processDonations();
                displayDonations();

                // This code ends the do while to run again
                System.out.print("Enter yes if you want to run again: ");
                anotherCourse = input.next();
                input.nextLine(); // causes skipping issue to fix
                System.out.print("\n\n\n");
            } while (anotherCourse.equalsIgnoreCase("yes"));

        } // end of main

        public static void getDonations() {
            Scanner input = new Scanner(System.in); // needed for input
            // Prompt user for site info
            for (int i = 0; i < 6; i++) {
                System.out.println("Enter site " + (i + 1) + " name: ");
                siteName[i] = input.next();

                System.out.println("Enter cash donation(USD) for " + siteName[i]
                        + ": ");
                cashDonations[i] = input.nextDouble();
                // double [] cashDonations = new double [6]; You have this already
                // at the top
                // int i;
                // for (i = 0 ; i < 6 ; i++)

                // cashDonations[i] = input.nextDouble();

                // double [] lbsFood = new double [6];
                System.out.println("Enter food donation(lbs.) for " + siteName[i]
                        + ": ");
                lbsFood[i] = input.nextDouble();

            }

        }

        public static void processDonations() {
            totalCash = 0;
            totalFood = 0;
            maxCash = cashDonations[0];
            maxFood = lbsFood[0];

            totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3]
                    + cashDonations[4] + cashDonations[5] + cashDonations[6];
            totalFood = lbsFood[1] + lbsFood[1] + lbsFood[2] + lbsFood[3]
                    + lbsFood[4] + lbsFood[5] + lbsFood[6];

        }

        public static void displayDonations() {
            System.out.print("Total Cash Donations are " + totalCash);
            System.out.print("Total Food Donations are " + totalFood);
            System.out.print("Donation totals for " + siteName[1]);
            System.out.print("Total cash: " + cashDonations[1]);
            System.out.print("Food donations " +lbsFood[1]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[2]);
            System.out.print("Total cash: " + cashDonations[2]);
            System.out.print("Food donations " +lbsFood[2]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[3]);
            System.out.print("Total cash: " + cashDonations[3]);
            System.out.print("Food donations " +lbsFood[3]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[4]);
            System.out.print("Total cash: " + cashDonations[4]);
            System.out.print("Food donations " +lbsFood[4]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[5]);
            System.out.print("Total cash: " + cashDonations[5]);
            System.out.print("Food donations " +lbsFood[5]);
            System.out.println();
            System.out.print("Donation totals for " + siteName[6]);
            System.out.print("Total cash: " + cashDonations[6]);
            System.out.print("Food donations " +lbsFood[6]);
            System.out.println();

        }// end of displayDonations()

    }// end of class

共 (3) 个答案

  1. # 1 楼答案

    代码中有几个地方直接引用代码中的索引6。以下是一些:

    System.out.print("Donation totals for " + siteName[6]);
    System.out.print("Total cash: " + cashDonations[6]);
    System.out.print("Food donations " +lbsFood[6]);
    

    但是所有数组都声明为长度6

    static double[] cashDonations = new double[6]; // stores the cash donations
                                                        // from 6 sites
    static double[] lbsFood = new double[6]; // stores the pounds of food
                                                    // donated from 6 sites
    static String[] siteName = new String[6];
    

    Arrays in Java are 0-based,所以长度为n的数组只有0n - 1的索引,或者这里是05的索引。如果需要索引6,请将数组长度设置为7

  2. # 2 楼答案

    我注意到你犯了一个常见的“一个接一个”的错误\_(ツ)_/“'

    在这里,您正确地声明了3个大小为6的数组:

        static double[] cashDonations = new double[6]; 
    
        static double[] lbsFood = new double[6]; 
    
        static String[] siteName = new String[6];
    

    虽然每个数组中确实有6个元素,但第一个元素被称为0

    [0]、[1]、[2]、[3]、[4]、[5]

    在代码中,您调用了不存在的第七个元素“[6]”:

       totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3]
                    + cashDonations[4] + cashDonations[5] + cashDonations[6];
    
       totalFood = lbsFood[1] + lbsFood[1] + lbsFood[2] + lbsFood[3]
                    + lbsFood[4] + lbsFood[5] + lbsFood[6];
    

    要修复它,只需将元素从0设置为5:

       totalCash = cashDonations[0] + cashDonations[1] + cashDonations[2]
                    + cashDonations[3] + cashDonations[4] + cashDonations[5];
    
       totalFood = lbsFood[0] + lbsFood[1] + lbsFood[2]
                    + lbsFood[3] + lbsFood[4] + lbsFood[5];
    

    这是一个容易犯的错误,因为我们被教导,0在我们的一生中毫无价值

    继续编程,不要放弃

  3. # 3 楼答案

    static double[] cashDonations = new double[6];
    

    这是一个有6个空格的数组,对吗。然而:

    maxCash = cashDonations[0];
    totalCash = cashDonations[1] + cashDonations[2] + cashDonations[3] + cashDonations[4] + cashDonations[5] + cashDonations[6];
    

    在这里,您将访问7个空间。0,1,2,3,4,5和6