1,编写一个学生类,类名为Student 成绩为数组并且给他赋值
答案:1 悬赏:40 手机版
解决时间 2021-01-28 06:51
- 提问者网友:niaiwoma
- 2021-01-27 15:41
1,编写一个学生类,类名为Student 成绩为数组并且给他赋值
最佳答案
- 五星知识达人网友:山有枢
- 2021-01-27 17:19
class Student{
private String name;
private String ID;
private double[] score;
public Student(String name, String ID){
this.name = name;
this.ID = ID;
}
public String getName(){
return name;
}
public String getID(){
return ID;
}
public boolean setScore(double fenshu[]){
boolean set = true;
score = new double[fenshu.length];
for(int i = 0; i < fenshu.length; i++){
if(fenshu[i] < 0 || fenshu[i] > 100){
set = false;
}else{
score[i] = fenshu[i];
}
}
return set;
}
public double[] getScore(){
return score;
}
public double sum(){
double sum = 0;
for(int i = 0; i < score.length; i++){
sum += score[i];
}
return sum;
}
public double average(){
double average = this.sum() / score.length;
return average;
}
public double[] max_min(){
double[] max_min = new double[2];
max_min[0] = 0;
max_min[1] = score[0];
for(int i = 0; i < score.length; i++){
if(score[i] > max_min[0]){
max_min[0] = score[i];
}
if(score[i] < max_min[1]){
max_min[1] = score[i];
}
}
return max_min;
}
public void prinf(){
System.out.println(name + ID +"成绩的总分是:" + this.sum());
System.out.println(" 成绩的平均分是: " + this.average());
System.out.println(" 最高分是:"+ this.max_min()[0]);
System.out.println(" 最低分是:"+ this.max_min()[1]);
}
}
public class TestStudent{
public static void main(String[] args){
Student std = new Student("张三", "201607");
double[] fenshu ={56, 77, 89};
if(std.setScore(fenshu)) std.prinf();
}
}
private String name;
private String ID;
private double[] score;
public Student(String name, String ID){
this.name = name;
this.ID = ID;
}
public String getName(){
return name;
}
public String getID(){
return ID;
}
public boolean setScore(double fenshu[]){
boolean set = true;
score = new double[fenshu.length];
for(int i = 0; i < fenshu.length; i++){
if(fenshu[i] < 0 || fenshu[i] > 100){
set = false;
}else{
score[i] = fenshu[i];
}
}
return set;
}
public double[] getScore(){
return score;
}
public double sum(){
double sum = 0;
for(int i = 0; i < score.length; i++){
sum += score[i];
}
return sum;
}
public double average(){
double average = this.sum() / score.length;
return average;
}
public double[] max_min(){
double[] max_min = new double[2];
max_min[0] = 0;
max_min[1] = score[0];
for(int i = 0; i < score.length; i++){
if(score[i] > max_min[0]){
max_min[0] = score[i];
}
if(score[i] < max_min[1]){
max_min[1] = score[i];
}
}
return max_min;
}
public void prinf(){
System.out.println(name + ID +"成绩的总分是:" + this.sum());
System.out.println(" 成绩的平均分是: " + this.average());
System.out.println(" 最高分是:"+ this.max_min()[0]);
System.out.println(" 最低分是:"+ this.max_min()[1]);
}
}
public class TestStudent{
public static void main(String[] args){
Student std = new Student("张三", "201607");
double[] fenshu ={56, 77, 89};
if(std.setScore(fenshu)) std.prinf();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯