class subject{
int credit;
int score;
public subject
(String name,
int credit,
int score) {
this.name = name;
this.credit = credit;
this.score = score;
}
void printData()
{
System.
out.
println("學分"+credit
); System.
out.
println("成績"+score
); System.
out.
println("++++++++++++++++"); }
}
public class Ex1 {
static double av(subject X[]){
int sum=0;
double credits=0;
for(int i=0; i<X.length; i++){
sum+=X[i].credit*X[i].score;
credits+=X[i].credit;
}
return sum/credits;
}
public static void main
(String[] args
) { subject X[]=new subject[3];
X[0]=new subject("程式設計", 4, 67);
X[1]=new subject("國文", 2, 77);
X[2]=new subject("英文", 2, 67);
for(int i=0; i<X.length; i++)
X[i].printData();
System.
out.
println("平均:"+av
(X
)); }
}