簡體   English   中英

錯誤消息,指出已超出陣列

[英]Error message saying that the array has been exceeded

我想知道你們是否可以給我有關如何修復我的代碼的指示。 我試圖發布一條錯誤消息,當您輸入太多數字時,數組的大小已超出。 我知道我為此寫了兩篇文章,很多人告訴我要具體,由我自己做,我決定由我自己做這個程序,而不是尋求幫助。 因此,我編寫了代碼,結果很不錯,但是當它說“輸入數字11:”時我將如何處理,然后輸入一個數字,並說它已被超出並在磁盤上打印出10個數組。下一行。

輸入:

import java.util.Scanner;

 public class FunWithArrays
{
    public static void main(String[] args)
    {
        final int ARRAY_SIZE = 11; // Size of the array

        // Create an array.
        int[] numbers = new int[ARRAY_SIZE];

        // Pass the array to the getValues method.
        getValues(numbers);

        System.out.println("Here are the " + "numbers that you entered:");

        // Pass the array to the showArray method.
        showArray(numbers);
    }

    public static void getValues(int[] array)
    {
        // Create a Scanner objects for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a series of " + array.length + " numbers.");

        // Read the values into the array
        for (int index = 0; index < array.length; index++)
        {
        // To tell users if they exceeded over the amount
            if (index > 9)
            {
                System.out.print("You exceeded the amount " + " ");
            }
            else
            {
            System.out.print("Enter the number " + (index + 1) + ": ");
            array[index] = keyboard.nextInt();
            }
        }
    }

    public static void showArray(int[] array)
    {
        // Display the array elements.
        for (int index = 0; index < array.length; index++)
            System.out.print(array[index] + " ");

    }
}

輸出:

Enter a series of 11 numbers.
Enter the number 1: 3321
Enter the number 2: 3214
Enter the number 3: 213
Enter the number 4: 21
Enter the number 5: 321
Enter the number 6: 321
Enter the number 7: 3
Enter the number 8: 213
Enter the number 9: 232
Enter the number 10: 321
You exceeded the amount  Here are the numbers that you entered:
3321 3214 213 21 321 321 3 213 232 321 0

好的,這是您需要的代碼,請記住,第11個元素根本沒有放入數組中。

  public static void main(String[] args) {
        final int ARRAY_SIZE = 11; // Size of the array

        // Create an array.
        int[] numbers = new int[ARRAY_SIZE];

        // Pass the array to the getValues method.
        getValues(numbers);

        System.out.println("Here are the " + "numbers that you entered:");

        // Pass the array to the showArray method.
        showArray(numbers);
    }

    public static void getValues(int[] array) {
        // Create a Scanner objects for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a series of " + array.length + " numbers.");

        // Read the values into the array
        for (int index = 0; index < array.length; index++) {
            // To tell users if they exceeded over the amount
            if (index >= 10) {
                System.out.print("Enter the number " + (index + 1) + ": ");
                array[index] = keyboard.nextInt();
                System.out.println("\nYou exceeded the amount " + " ");

            } else {
                System.out.print("Enter the number " + (index + 1) + ": ");
                array[index] = keyboard.nextInt();
            }
        }
    }

    public static void showArray(int[] array) {
        // Display the array elements.
        for (int index = 0; index < array.length-1; index++) {
            System.out.print(array[index] + " ");
        }

    }
}

我不知道為什么要這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM