java中关于数组作函数的疑问 本帖最后由 lxmxrx 于 2010-04-20 14:51:12 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Student student = new Student(studentName, course[5],score[5]);-->Student student = new Student(studentName, course,score); 正解course[5],score[5]这两个参数传错了,应该传递数组的引用(即数组名).你把course[5]当成整个数组了,其实couuse[int x]这只是数组里的一个值。 上面有很多错误,重新编译后是这样的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(); }} 楼主在玩人吧!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 出来? 你使用的是什么工具?难道这样的代码不报错? 对不起,各位,刚才是在UtralEdit中写出来的。没有编译过。有很多错误。已经修改了。4楼为正解。 怎样用Java获得Windows系统的应用程序列表?? 大家看看这个程序请教为什么图片不出来的呢 JAVA常用包. java字符串拆分 各位大哥,小弟又有难了,乞求大哥们拔刀相助,关于BS和CS◎◎◎◎◎◎◎◎◎◎◎◎◎(100分求解) java皮肤怎么用, 请教:下载的mysql数据库驱动程序mm.mysql-2.0.4-bin.jar应该放在哪个目录下 大家来看看是不是oracle jdbc的bug? 谁能帮帮新手啊!!!!! 学到了maven,想问下jvm类加载的这个路径的问题 关于(输入输出)接收到的信息问题?? 切换程序后,原程序不工作了
course[5],score[5]这两个参数传错了,应该传递数组的引用(即数组名).你把course[5]当成整个数组了,其实couuse[int x]这只是数组里的一个值。
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(); }}
int[] score = new int{60,70,80,85,90};可以这样new 出来? 你使用的是什么工具?难道这样的代码不报错?