我尝试开发一个程序来动态地收集学生数据,但是在运行时它会显示主类异常。这是我的代码:
import java.lang.reflect.Array;
import java.util.*;
public class Student {
String arr[]=null;
public static void main(String[] args) {
//int x=0;
String[] arr =new String[2];
Scanner input = new Scanner (System.in);
System.out.println("\t\t\tStudent Record Menu");
System.out.println("\t\tEnter Student Details");
int i=0;
for(i=0;i<arr.length;i++) {
System.out.println("Full name:");
String name = input.next();
Array.set(arr, i, name);
System.out.println("Age:");
String age = input.next();
Array.set(arr, i, age);
System.out.println("Course:");
String course = input.next();
Array.set(arr, i, course);
System.out.println("Year:");
String year = input.next();
Array.set(arr, i, year);
System.out.println("Section:");
String section = input.next();
Array.set(arr, i, section);
}
for(String s:arr){
System.out.println("The Values are:"+s.valueOf(i));
}
}
}
它没有显示任何错误,但它只是没有执行。请帮助我理解我必须在代码中更改的内容。
答案 0 :(得分:-1)
在您的示例中,您只存储该部分,因为您覆盖了以前存储的数据。 看输出..
Student Record Menu
Enter Student Details
Full name:
1
Age:
2
Course:
3
Year:
4
Section:
5
Full name:
6
Age:
7
Course:
8
Year:
9
Section:
0
The Values are:5
The Values are:0