import java.util.Scanner;
public class Student {// 带有主函数的类,Score类在下面
String name;
Score score = new Score();// 此句有重大嫌疑 nested class object as an attribute, no mistake here!
public String introduce() {
return (name + "\t" + score.java + "\t" + score.db + "\t" + score.c
+ "\t" + score.sum() + "\t" + score.avg());// name之后的全部显示为0
}
public static void main(String args[]) {
Student stu[] = new Student[10];
// Score score[] = new Score[10];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
stu[i] = new Student();
// score[i] = new Score();
System.out.println("学生" + (i + 1) + "的名字:");
stu[i].name = in.next();
System.out.println("学生" + (i + 1) + "java成绩:");
stu[i].score.java = in.nextFloat();
// score[i].java = in.nextFloat();
System.out.println("学生" + (i + 1) + "DB成绩:");
// score[i].db = in.nextFloat();
stu[i].score.db = in.nextFloat();
System.out.println("学生" + (i + 1) + "C语言成绩:");
stu[i].score.c = in.nextFloat();
// score[i].c = in.nextFloat();
}
System.out.println("姓名\tjava成绩\tDB成绩是\tc语言成绩\t总成绩\t平均成绩");
for (int j = 0; j < 3; j++) {
System.out.println(stu[j].introduce());
}
}
public class Score {// score类
float java, db, c;
public float sum() {
float ssum = java + db + c;
return ssum;
}
public float avg() {
return sum() / 3;
}
}
}
import java.util.Scanner;
public class Student {//带有主函数的类,Score类在下面
String name;
Score score=new Score();//此句有重大嫌疑
public String introduce()
{
return(name+"\t"+score.java+"\t"+score.db+"\t"+score.c+"\t"+score.sum()+"\t"+score.avg());//name之后的全部显示为0
}
public static void main(String args[])
{
Student stu[]=new Student[10];
Score score[]=new Score[10];
Scanner in=new Scanner(System.in);
for(int i=0;i<3;i++)
{
stu[i]=new Student();
score[i]=new Score();
System.out.println("学生"+(i+1)+"的名字:");
stu[i].name=in.next();
System.out.println("学生"+(i+1)+"java成绩:");
score[i].java=in.nextFloat();
System.out.println("学生"+(i+1)+"DB成绩:");
score[i].db=in.nextFloat();
System.out.println("学生"+(i+1)+"C语言成绩:");
score[i].c=in.nextFloat();
}
System.out.println("姓名\tjava成绩\tDB成绩是\tc语言成绩\t总成绩\t平均成绩");
for(int j=0;j<3;j++)
{
System.out.println(stu[j].introduce());
}
}
} //错误之二:少了一个右边大括号,造成第一个类不完整
class Score {//score类 错误地方一:在一个java文件中只允许有一个功能类 去掉public 就好了
float java,db,c;
public float sum()
{
float ssum=java+db+c;
return ssum;
}
public float avg()
{
return sum()/3;
}
}
一般来说会用两种最常用的方式
new一个对象,通过对象来调用类中的变量和方法
如果一个类的方法是静态的,那么直接用类名.方法的方式就可以调用
for(int i=0;i<3;i++)
{
stu[i]=new Student();
score[i]=new Score();
System.out.println("学生"+(i+1)+"的名字:");
stu[i].name=in.next();
System.out.println("学生"+(i+1)+"java成绩:");
score[i].java=in.nextFloat();
System.out.println("学生"+(i+1)+"DB成绩:");
score[i].db=in.nextFloat();
System.out.println("学生"+(i+1)+"C语言成绩:");
score[i].c=in.nextFloat();
}
改成:
for(int i=0;i<3;i++)
{
stu[i]=new Student();
System.out.println("学生"+(i+1)+"的名字:");
stu[i].name=in.next();
System.out.println("学生"+(i+1)+"java成绩:");
stu[i].score.java=in.nextFloat();
System.out.println("学生"+(i+1)+"DB成绩:");
stu[i].score.db=in.nextFloat();
System.out.println("学生"+(i+1)+"C语言成绩:");
stu[i].score.c=in.nextFloat();
}
public class Score {//score类
public Score(float java,float db,float c) {
this.java =java;
this.db = db ;
this.c = c;
}
float java,db,c;
public float sum()
{
float ssum=java+db+c;
return ssum;
}
public float avg()
{
return sum()/3;
}
}
替换你原来的
然后main方法里 new Score()改成 new Score(java值,db值,c值);