繁体   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