Java编程题:
- 定义一个Student类,包含属性:分数,总分, 人数;
- 成员函数用于设置分数、求总分累计学生人数;sum()返回总分,average()用于求平均值
Student类:
/**
* Java编程题:
* 定义一个Student类,包含属性:分数,总分, 人数;
* 成员函数用于设置分数、求总分累计学生人数;sum()返回总分,average()用于求平均值
*/
package Student;
public class Student {
/*
属性
*/
//公有属性:分数
public float score;
//公有静态属性:总分
public static float total;
//公有静态属性:学生人数
public static int count;
/*
设置方法
*/
//赋值
public void scoretoalcount(float s){
this.score = s;
count = (int)s;
total = s * count;
}
/*
方法
*/
//返回总分
public float sum(){
return total;
}
//返回平均值
public float average(){
return total / count;
}
}
Student测试类:
/**
* Student测试类
*/
package Student;
public class StudentText {
public static void main(String[] args) {
//构建学生
Student student = new Student();
//设置分数,总分,人数
student.scoretoalcount(3);
//显示总分
System.out.println("总分:" + student.sum());
//显示平均值
System.out.printf("平均值:" + student.average());
}
}
结果:
总分:9.0
平均值:3.0
进程已结束,退出代码为 0
感谢观看
再次感谢~