Мне было интересно, если вы, ребята, можете дать мне советы о том, как исправить мой код.Я пытаюсь выдать сообщение об ошибке, что размер массива был превышен, когда вы ввели слишком много чисел.Я знаю, что написал два поста об этом, и многие люди сказали мне быть конкретным и сделать это самостоятельно, и я решил сделать эту программу самостоятельно, а не просить о помощи.Итак, я написал код, и он получился хорошим, но как бы я это сделал, когда он говорит: «Введите число 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