简体   繁体   English

错误消息,指出已超出阵列

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

I was wondering if you guys can give me pointers on how to fix my code. 我想知道你们是否可以给我有关如何修复我的代码的指示。 I am trying to put an out an error message that the size of the array has been exceeded when you entered too many numbers. 我试图发布一条错误消息,当您输入太多数字时,数组的大小已超出。 I know I wrote two posts about this, and many people told me to be specific and do it by myself and I decided to do this program by myself instead of asking for help. 我知道我为此写了两篇文章,很多人告诉我要具体,由我自己做,我决定由我自己做这个程序,而不是寻求帮助。 So I wrote the code, and it came out nice, but how would I do it when it says, "Enter the number 11:" then I enter a number, and it says it has been exceeded and prints out the 10 arrays on the next line. 因此,我编写了代码,结果很不错,但是当它说“输入数字11:”时我将如何处理,然后输入一个数字,并说它已被超出并在磁盘上打印出10个数组。下一行。

Input: 输入:

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] + " ");

    }
}

output: 输出:

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

OK, here is the code to your need, bare in mind that 11th element is not put into an array at all. 好的,这是您需要的代码,请记住,第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] + " ");
        }

    }
}

And I have no idea, why do you want it like this. 我不知道为什么要这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Travis 中的错误:作业超过了最大日志长度,并且已终止 - Error in Travis: The job exceeded the maximum log length, and has been terminated javac给出一条错误消息,表明已找到 - javac gives an error message , that it has been found 我在 IntelliJ 上遇到一个错误,提示无法创建带有奇怪错误消息的类 - I've been getting an error on IntelliJ saying Cannot Create Class with an odd Error message 我一直收到错误,说 D1D 尚未定义? - I keep getting an error, saying that D1D has not been defined? 错误声明 byte[] 数组副本的 ArrayList 返回了欺骗性错误消息,说要使用迭代器 - Error declaring an ArrayList of byte[] array copy returned deceptive error message saying to use iterator 条件表达式:错误消息说明类型不完整 - Conditional Expressions: Error message saying incomplete types Elasticsearch 异常:已超出索引中的映射深度 - Elasticsearch exception : mappping depth in index has been exceeded Android线程/处理程序错误IllegalStateException:尚未发布指定的消息队列同步障碍标记 - Android Thread/Handler Error IllegalStateException: The specified message queue synchronization barrier token has not been posted Apache IVY错误消息? :数据未加载时无法获取工件 - Apache IVY error message? : impossible to get artifacts when data has not been loaded 线程/处理程序错误 - 尚未发布指定的消息队列同步屏障标记 - Thread/Handler error - The specified message queue synchronization barrier token has not been posted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM