当我输入1条记录并选择1。添加记录时,它总是有错误。当我输入2条记录时,我只能输入1条记录,当我选择1。再次添加记录时,它会显示OutofBounds。我该如何解决该错误?
public static int recno=0, recsize, choice, i=0;
public static void main (String args[]) throws IOException{
System.out.print("Enter Number of Records");
recsize = Integer.parseInt(reader.readLine());
String EmpNo[] = new String[recsize];
display_menu(EmpNo,recno);
}
public static void add_Rec(String EmpNo[], int recno) throws IOException{
++recno;
EmpNo[recno]= "EMP-"+ recno;
System.out.print("Employee Number: " + EmpNo[recno]);
System.out.print("\nEmployee Name: ");
display_menu(EmpNo,recno);
}
public static void display_menu(String EmpNo[], int recno) throws IOException{
System.out.println("Main Menu");
System.out.println("1. Add record");
System.out.println("Enter Your Choice");
choice = Integer.parseInt(reader.readLine());
if (choice==1){
add_Rec(EmpNo,recno);
}
}
最佳答案
您应该替换以下内容:
++recno;
EmpNo[recno] = "EMP-" + recno;
带有:
if (recno < recsize) {
EmpNo[recno++] = "EMP-" + recno;
// ...
}