我试图在另一个方法(2)中调用方法(1)。但是在2个方法中调用1个方法后,其余的代码行不能在2个方法中工作。解决方案请。
第一个方法
public static void student(){
Scanner input = new Scanner(System.in);
String[] student_id=new String[150];
System.out.print("Enter Student ID : ");
for(int i =0;i<student_id.length;i++){
student_id[i] = input.nextLine();
String[] student_name=new String[150];
System.out.print("Enter Student Name : ");
for(int j =0;j<student_name.length;j++){
student_name[j] = input.nextLine();
}
}}
第二种方法
public static void addNewStudent(){
Scanner input = new Scanner(System.in);
System.out.println("-------------------------------------------------------------------------------------");
System.out.println("| ADD NEW STUDENT |");
System.out.print("-------------------------------------------------------------------------------------n");
System.out.println();
student(); //calling 1 method.rest of codes are not working.
System.out.println();
System.out.print("Student has been added successsfully. Do you want to add a new student (y/n): ");
String yn = input.nextLine();
if (yn.equals("y")){
clearConsole();
addNewStudent();
} else {
clearConsole();
homePage();
}
}
外循环运行150
次并读取一行,内循环运行150 * 150
次并读取一行。总共要读取150 + 150 * 150 = 22650
行。
那么问题是为什么一个学生会有150个id和22500个名字。
逻辑中肯定有一个缺陷,因为addNewStudent()递归地处理新生,甚至在不需要更多学生时也不返回。