java中关于数组作函数的疑问

夜半微凉 2010-04-20 02:50:39

class Student {
private String studentName;
private String[] courseName;
private int[] courseScore;
public Student(String studentName, String[] courseName,int[] courseScore;) {
this.studentName = studentName;
this.courseName = courseName;
this.courseScore = courseScore;
}
private int sum() {
int sum = 0;
for(int i=0; i<courseScore.length; i++) {
sum += courses[i];
}
return sum;
}
private double avg() {
return this.sum() / courses.length;
}
public void printStudentInfo() {
System.out.println("StudentName:\t\t" + studentName);
for(int i=0; i<courseScore.length; i++) {
System.out.println(courseName[i] + ":\t\t" + courseScore[i]);
}
System.out.println("TotalScore:\t\t" + this.sum());
System.out.println("Average:\t\t" + this.avg());
}
}
public class MainClass {
public static void main(String[] args) {
String studentName = "James.King";
Course courseA = new Course("courseA", 60);
Course courseB = new Course("courseB", 70);
Course courseC = new Course("courseC", 80);
Course courseD = new Course("courseD", 90);
Course courseE = new Course("courseE", 100);
String[] course = new String{"courseA","courseB","courseC","courseD","courseE"};
int[] score = new int{60,70,80,85,90};
Student student = new Student(studentName, course[5],score[5]);
student.printStudentInfo();
}
}



Student student = new Student(studentName, course[5],score[5]);
为什么调用该构造方法时无法解析?
...全文
94 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
So_So 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lxmxrx 的回复:]
对不起,各位,刚才是在UtralEdit中写出来的。没有编译过。有很多错误。已经修改了。4楼为正解。
[/Quote]
那你对得起4楼吗? 结贴像你这样结贴的? 别人把你代码重新给你搞好了 得分 0?

估计那哥们以后都不会看你的贴了 如果有 拉黑的功能 你估计就进去了
夜半微凉 2010-04-20
  • 打赏
  • 举报
回复
对不起,各位,刚才是在UtralEdit中写出来的。没有编译过。有很多错误。已经修改了。4楼为正解。
夜半微凉 2010-04-20
  • 打赏
  • 举报
回复
呵呵,晓得了
jacky花园 2010-04-20
  • 打赏
  • 举报
回复
汗~
lz
你传的是数组中的值
而你的构造是数组的嘛
还有比我更粗心的朋友哟~~``
So_So 2010-04-20
  • 打赏
  • 举报
回复
楼主在玩人吧!

public Student(String studentName, String[] courseName,int[] courseScore;)(你带个分号干吗?难道在你工程里面没报错!)

Student student = new Student(studentName, course[5],score[5]);

你的构造 怎么写的 放的是数组 你调用的时候 怎么的就用 数组的原数? 我真是佩服你! 玩人这样玩啊...

你String a[]=B[5]; 这样去玩玩吧 看看报错不!

String[] course = new String{"courseA","courseB","courseC","courseD","courseE"};
int[] score = new int{60,70,80,85,90};

可以这样new 出来? 你使用的是什么工具?难道这样的代码不报错?

夜半微凉 2010-04-20
  • 打赏
  • 举报
回复
上面有很多错误,重新编译后是这样的

public class Student {
private String studentName;
private String[] courseName;
private int[] courseScore;
public Student(String studentName, String[] courseName,int[] courseScore) {
this.studentName = studentName;
this.courseName = courseName;
this.courseScore = courseScore;
}
private int sum() {
int sum = 0;
for(int i=0; i<courseScore.length; i++) {
sum += courseScore[i];
}
return sum;
}
private double avg() {
return this.sum() / courseScore.length;
}
public void printStudentInfo() {
System.out.println("StudentName:\t\t" + studentName);
for(int i=0; i<courseScore.length; i++) {
System.out.println(courseName[i] + ":\t\t" + courseScore[i]);
}
System.out.println("TotalScore:\t\t" + this.sum());
System.out.println("Average:\t\t" + this.avg());
}

}
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String studentName = "James.King";
String[] course = new String[]{"courseA","courseB","courseC","courseD","courseE"};
int[] score = new int[]{60,70,80,85,90};
Student student = new Student(studentName, course[5],score[5]);
student.printStudentInfo();

}

}
youyouzhishen 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qery 的回复:]

Student student = new Student(studentName, course[5],score[5]);

-->

Student student = new Student(studentName, course,score);
[/Quote]
正解
course[5],score[5]这两个参数传错了,应该传递数组的引用(即数组名).你把course[5]当成整个数组了,其实couuse[int x]这只是数组里的一个值。
夜半微凉 2010-04-20
  • 打赏
  • 举报
回复
不能用数组传递吗?
qery 2010-04-20
  • 打赏
  • 举报
回复
Student student = new Student(studentName, course[5],score[5]);

-->

Student student = new Student(studentName, course,score);

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧