发布网友 发布时间:2024-10-23 22:26
共5个回答
热心网友 时间:2024-11-01 00:19
Student[] student = new Student[n];在这句的时候就出错了,你这个应该是想创建个Student类型的数组吧,这个地方的异常是com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.所以在student[i].number这里地方才会出空异常
我想你可以放到一个list里去比较,可能是你这个student的类型不能像int或String那样创建数组。我没这样写过,都是用list的,希望对你有帮助
热心网友 时间:2024-11-01 00:19
import java.util.Scanner;
public class NewClass {
public static void main(String args[]) {
NewClass application = new NewClass();
System.out.println("How many students do you want to input?");
Scanner str = new Scanner(System.in);
int n = str.nextInt();
Student[] student = new Student[n];
application.init(student);
application.sort(student);
application.output(student);
}
class Student {
public String number;
public String name;
public int scores;
public Student(String number, String name, int scores) {
this.number = number;
this.name = name;
this.scores = scores;
}
}
public void init(Student[] student) {
System.out.println("Please input " + student.length + " students' number,name and scores");
Scanner string = new Scanner(System.in);
for (int i = 0; i < student.length; i++) {
String number = string.nextLine();
String name = string.nextLine();
int scores = string.nextInt();
Student stu=new Student(number,name,scores);
student[i]=stu;
System.out.println("next one");
string = new Scanner(System.in);
}
}
public void sort(Student[] student) {
Student temp = new Student("3", "enen", 3);
for (int i = 0; i < student.length - 1; i++) {
for (int j = 1; j < student.length; j++) {
if (student[i].scores < student[j].scores) {
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
}
public void output(Student[] student) {
for (int i = 0; i < student.length; i++) {
System.out.println(student[i].name);
}
}
}
热心网友 时间:2024-11-01 00:18
额 肯定会报错的。。。Student[] student = new Student[n];这句话只是定义数组,里面并没有东西,你下面却直接使用
热心网友 时间:2024-11-01 00:16
没仔细看 不过你这个是写在一个 .java文件里的话那肯定出错 一个.java文件里只能有一个public的类
热心网友 时间:2024-11-01 00:16
你把init方法改成下面这样,试下:
public void init(Student[] student) {
System.out.println("Please input " + student.length + " students' number,name and scores");
Scanner string = new Scanner(System.in);
int i = 0;
while(string.hasNext() && i < student.length){
String[] str = string.nextLine().split(" ");
student[i] = new Student(str[0], str[1], Integer.parseInt(str[2]));
i++;
if(i == student.length){
return;
}
}
}