[英]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.