我正在制作一个数据库,您可以在其中输入动物和所需的补品。我必须使用多维数组。我遇到的问题是,当用户将数据输入到多维数组中时,我会遇到一个越界错误。我很困惑,因为我使用相同的方法让用户输入动物的类型和所需的补充剂的数量,但当它实际进入补充剂时,我介绍了并且数组保持以保持每只动物的补充剂举办。为了更直观的参考,我的逻辑想要像这样:
姓名 动物类型1补充1
动物类型沿着第一列向下,而补充物以每个动物的水平方式填充。我会发布我的代码,但特别是我遇到了数组越界的问题。我怀疑它与我如何初始化多阵列有关,但我现在还不确定。任何帮助将不胜感激!
//尼古拉斯·斯塔福德 // 2016年2月1日 //该程序允许用户输入最多任意数量的动物和饮食信息,然后允许用户在搜索数据库时显示该信息
import java.util.Scanner;
import java.util.Arrays;
公共类库存{
public static void main(String[] args)
{
//Initial variables
int choiceent;
int numAnimals = 0;
int numDiet = 0;
int ArrayHold = 0;
boolean isNum;
boolean isNum2;
boolean quit = false;
//Scanners needed for input
Scanner choice = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
//Initial value needed to enter animals into database
System.out.println("Enter the number of animals you wish to enter:");
do
{
if(input1.hasNextInt()) {
numAnimals = input1.nextInt();
isNum = true;
}
else {
System.out.println("Please enter an integer value!");
isNum = false;
input1.next();
}
}
while(!(isNum));
System.out.println("You are entering " +numAnimals+ " animals.");
//Multidimensional array
String [][] ZooBase = new String [100][100];
//Array for data types
int numDietA[] = new int [numAnimals];
do{
System.out.println("1. Enter the names of each animal");
System.out.println("2. Enter the dietary information");
System.out.println("3. Search the array for information");
System.out.println("4. Close the program");
choiceent = choice.nextInt();
switch(choiceent)
{
case 1:
//Data validation for name
for(int i = 0; i < numAnimals; i++)
{
System.out.println("Enter the type of animal for animal " +i+ "");
while(!input2.hasNext("[a-zA-Z]+"))
{
System.out.println("Enter a type of animal!");
ZooBase[i+1][0] = input2.nextLine();
}
ZooBase[i+1][0] = input2.nextLine();
}
//Display Names
System.out.println("Names");
for(int j = 0; j < numAnimals; j++){
System.out.println(ZooBase[j+1][0]);
}
break;
case 2:
for(int j = 0; j < numAnimals; j++)
{
System.out.println("How many supplements does the " +ZooBase[j+1][0]+ " need?");
do
{
if(input3.hasNextInt()) {
numDietA[j] = input3.nextInt();
isNum2 = true;
}
else {
System.out.println("Please enter an integer value!");
isNum2 = false;
input3.next();
}
}while(!(isNum2));
}
for(int k = 0; k < numAnimals; k++)
{
ArrayHold = k+1;
for(int m = 0; m < numDietA[m]; m++ )
{
System.out.println("Enter item " +m+ " for the " +ZooBase[m+1][0]+ "");
while(!input4.hasNext("[a-zA-Z]+"))
{
System.out.println("Enter a supplement (No integers)!");
ZooBase[ArrayHold][m+2] = input4.nextLine();
}
ZooBase[ArrayHold][m+2] = input4.nextLine();
}
}
break;
case 3:
break;
case 4:
quit = true;
break;
default:
System.out.println("Invalid option!");
break;
}
}while(choiceent != 4);
}
}
答案 0 :(得分:0)
初始化数组后,其大小无法更改。要确保不会发生这种情况,请使用没有固定边界的ArrayList&lt;&gt ;.您可以在此处找到语法+所有详细信息:https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
希望有所帮助!
PS。你也可以只初始化1个扫描仪输入并在整个
中使用它