gpt4 book ai didi

Java 越界错误

转载 作者:行者123 更新时间:2023-12-02 05:34:49 24 4
gpt4 key购买 nike

当我输入 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;
// ...
}

关于Java 越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com