import java.text.DecimalFormat;
class student //学生类
{
String stuNo;
String stuName;
String stuSex;
boolean banganbu;
double math;
double chinese;
double english;
void input(String sNo,String sName,String sex,boolean bgb,double math1,double chinese1,double english1) //input成员函数
{ // 类的成员函数接收实际参数,给类的数据成员赋值
stuNo=sNo;
stuName=sName;
stuSex=sex;
banganbu=bgb;
math=math1;
chinese=chinese1;
english=english1;
}
double sum_score()
{
double sum;
sum=math+chinese+english;
return sum;
}
double avg()
{
double average;
average=sum_score()/3;
return average;
}
}
public class p94_13 {
public static void main(String []args)
{
student stu=new student(); //用类student创建对象stu
//Scanner reader=new Scanner(System.in);
stu.input("1001","zhangsan","m",true,85.5,90,88); //直接赋值(只是一个学生的情况)
double sum=stu.sum_score(); //用对象调用类的成员函数sum_score()
double avg=stu.avg(); //用对象调用成员函数avg()
DecimalFormat df = new DecimalFormat("#.0"); //平均分保留一位小数
System.out.println("总分:"+sum + " 平均分:"+df.format(avg));
}
}
运行结果: