下面这个java代码,那里有错误,怎么改正啊?
publicclassStudent{privateStringid;//学号privateStringname;//姓名privateintengl;//英语成绩pri...
public class Student {
private String id; // 学号
private String name; // 姓名
private int engl; // 英语成绩
private int math; // 数学成绩
private int comp; // 计算机成绩
private double aver; // 平均分
private Student students[]; // 学生
private int size; // 人数
Student(int size){
this.size=size;
};
Student(String id, String name, int engl, int math, int comp) {
this.id = id;
this.name = name;
this.engl = engl;
this.math = math;
this.comp = comp;
aver = average();
}
public Student(Student s){
this.id=s.id;
this.name=new String(s.name);
this.engl=s.engl;
this.math=s.math;
this.comp=s.comp;
this.aver=s.average();
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
for (int i = 0; i < size; i++)
this.students[i] = new Student(students[i]);
}
double average() {
return (engl + math + comp) / 3;
}
String getId() {
return id;
}
String getName() {
return name;
}
int getEnglish() {
return engl;
}
int getMath() {
return math;
}
int getComputer() {
return comp;
}
double getAverage() {
return aver;
}
public String toString() {
String s;
s = "学号" + "\t" + "姓名" + "\t" + "英语" + "\t" + "数学" + "\t" + "计算机"
+ "\t" + "平均分\n";
for (int i = 0; i <size; i++)
s = s + students[i].getId() + "\t" + students[i].getName() + "\t "
+ students[i].getEnglish() + "\t" + students[i].getMath()
+ "\t" + students[i].getComputer() + "\t"
+ students[i].getAverage() + "\n";
return s;
}
}
import java.util.*;
public class textStudent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student students[];
students = new Student[5];
for (int i = 0; i < 5; i++) {
students[i] = new Student(getAStudent(i + 1));
}
Student stu = new Student(5);
stu.setStudents(students);
System.out.print(stu);
}
public static Student getAStudent(int i) {
Scanner Keyboard = new Scanner(System.in);
Student studenti;
System.out.println("输入第" + i + "个学生的信息:");
System.out.print("学号:");
String id = Keyboard.next();
System.out.print("姓名:");
String name = Keyboard.next();
System.out.print("英语成绩:");
int eng = Keyboard.nextInt();
System.out.print("数学成绩:");
int math = Keyboard.nextInt();
System.out.print("计算机成绩:");
int comp = Keyboard.nextInt();
studenti = new Student(id, name, eng, math, comp);
return studenti;
}
} 展开
private String id; // 学号
private String name; // 姓名
private int engl; // 英语成绩
private int math; // 数学成绩
private int comp; // 计算机成绩
private double aver; // 平均分
private Student students[]; // 学生
private int size; // 人数
Student(int size){
this.size=size;
};
Student(String id, String name, int engl, int math, int comp) {
this.id = id;
this.name = name;
this.engl = engl;
this.math = math;
this.comp = comp;
aver = average();
}
public Student(Student s){
this.id=s.id;
this.name=new String(s.name);
this.engl=s.engl;
this.math=s.math;
this.comp=s.comp;
this.aver=s.average();
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
for (int i = 0; i < size; i++)
this.students[i] = new Student(students[i]);
}
double average() {
return (engl + math + comp) / 3;
}
String getId() {
return id;
}
String getName() {
return name;
}
int getEnglish() {
return engl;
}
int getMath() {
return math;
}
int getComputer() {
return comp;
}
double getAverage() {
return aver;
}
public String toString() {
String s;
s = "学号" + "\t" + "姓名" + "\t" + "英语" + "\t" + "数学" + "\t" + "计算机"
+ "\t" + "平均分\n";
for (int i = 0; i <size; i++)
s = s + students[i].getId() + "\t" + students[i].getName() + "\t "
+ students[i].getEnglish() + "\t" + students[i].getMath()
+ "\t" + students[i].getComputer() + "\t"
+ students[i].getAverage() + "\n";
return s;
}
}
import java.util.*;
public class textStudent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student students[];
students = new Student[5];
for (int i = 0; i < 5; i++) {
students[i] = new Student(getAStudent(i + 1));
}
Student stu = new Student(5);
stu.setStudents(students);
System.out.print(stu);
}
public static Student getAStudent(int i) {
Scanner Keyboard = new Scanner(System.in);
Student studenti;
System.out.println("输入第" + i + "个学生的信息:");
System.out.print("学号:");
String id = Keyboard.next();
System.out.print("姓名:");
String name = Keyboard.next();
System.out.print("英语成绩:");
int eng = Keyboard.nextInt();
System.out.print("数学成绩:");
int math = Keyboard.nextInt();
System.out.print("计算机成绩:");
int comp = Keyboard.nextInt();
studenti = new Student(id, name, eng, math, comp);
return studenti;
}
} 展开
2个回答
展开全部
楼上说的有问题,
for (int i = 0; i < size; i++)
你用的是size作为条件控制条件,如果用students.length来初始数组size条件不就没用了么
应该在
Student(int size){
this.size=size;//这里你光获取长度了,而没有对本地数组students初始化
}
改为
Student(int size){
this.size=size;
students=new Student[size];
}
并且在
public void setStudents(Student[] students) {
for (int i = 0; i < size; i++)
添加判断条件
if(size>=students.length)
保证用来赋值的数组长度不超过本地数组长度
for (int i = 0; i < size; i++)
你用的是size作为条件控制条件,如果用students.length来初始数组size条件不就没用了么
应该在
Student(int size){
this.size=size;//这里你光获取长度了,而没有对本地数组students初始化
}
改为
Student(int size){
this.size=size;
students=new Student[size];
}
并且在
public void setStudents(Student[] students) {
for (int i = 0; i < size; i++)
添加判断条件
if(size>=students.length)
保证用来赋值的数组长度不超过本地数组长度
更多追问追答
追问
Student中添加的方法:用来增加一个方法
public void addStudent(Student addstudent){ //增加学生
students[size]=addstudent;
}
主方法中调用如下:
System.out.print("增加学生");
stu.addStudent(getAStudent(i+1));
System.out.println("添加成功");
有什么问题啊?
追答
你的getAStudent是想取第i个学生么?那应该是i-1,才能返回第i个student
还有students[size]=addstudent;
这样你的size是固定的,总改的那一个数组元素
而且students[size]超出数组范围了,长度为size的数组,下标最多为size-1
而且跟上面的是一样的,需要先对students数组初始化
如果你的意思是size始终指向数组尾部进行添加,请改用动态数组,你用的students[]是不能这么用的
展开全部
在 setStudents()这个函数的for循环语句前面加一句:
this.students = new Student[students.length];
说下这个问题,在定义属性的时候,有Student students[];但是students只是声明了,但是多少个还没有初始化。
public void setStudents(Student[] students){
this.students = new Student[students.length];
for (int i = 0; i < students.length; i++)
this.students[i] = new Student(students[i]);
}
this.students = new Student[students.length];
说下这个问题,在定义属性的时候,有Student students[];但是students只是声明了,但是多少个还没有初始化。
public void setStudents(Student[] students){
this.students = new Student[students.length];
for (int i = 0; i < students.length; i++)
this.students[i] = new Student(students[i]);
}
追问
Student中添加的方法:用来增加一个方法
public void addStudent(Student addstudent){ //增加学生
students[size]=addstudent;
}
主方法中调用如下:
System.out.print("增加学生");
stu.addStudent(getAStudent(i+1));
System.out.println("添加成功");
有什么问题啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询