1新建一个学生Student类,属性包括学号、姓名、数学、英语和计算机三门课程成绩及平均分。 2.新建一个学生班级类StudentClass,包含变量一个学生数组长度为5,代表5名学生Student; 3.在构造方法中新建5位学生的基本信息,要求可以通过键盘输入中修改学生除学号、平均分之外的信息; 4.要求该类具有计算学生平均分并最终输出每位学生的学号、姓名、平均分。
package ex8;
/**
* @content:新建一个学生Student类,属性包括学号、姓名、数学、英语和计算机三门课程成绩及平均分。
* @author:卡面来打
*/
public class Student {
public int id;//学号
public String name;//姓名
public double Math;//数学成绩
public double English;//英语成绩
public double Computer;//计算机成绩
public double average;//学生平均分
public Student(int num, String name, double math, double english, double computer) {
super();
this.id = num;
this.name = name;
this.Math = math;
this.English = english;
this.Computer = computer;
}//构造方法结束
public int getNum() {
return id;
}
public void setNum(int num) {
this.id = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMath() {
return Math;
}
public void setMath(double math) {
Math = math;
}
public double getEnglish() {
return English;
}
public void setEnglish(double english) {
English = english;
}
public double getComputer() {
return Computer;
}
public void setComputer(double computer) {
Computer = computer;
}
public double getAverage() {
average =(English+Math+Computer)/3;
return average;
}
//重写toString方法输出结果
public String toString() {
return "学号="+id+",姓名="+name+",平均成绩="+getAverage();
}
}
package ex8;
import java.util.Scanner;
public class StudentClass{
public static void main(String[] args) {
Student[] stu=new Student[5];//定义一个长度为5的学生对象数组
Scanner sc=new Scanner(System.in);//从键盘中输入
for(int i=0;i<stu.length;i++) {
System.out.print("请输入第"+(i+1)+"个学生的学号:");
int id=sc.nextInt();
System.out.print("请输入第"+(i+1)+"个学生的姓名:");
String name=sc.next();
System.out.print("请输入英语成绩:");
double English=sc.nextDouble();
System.out.print("请输入数学成绩:");
double Math=sc.nextDouble();
System.out.print("请输入计算机成绩:");
double Computer=sc.nextDouble();
Student ss=new Student(id,name,English,Math,Computer);//学生类对象
stu[i]=ss;//把每个学生类对象放入对象数组中
}
sc.close();
//利用for循环简化语句输出对象数组的内容
for(Student j:stu) {
System.out.println(j);
}
}
}
运行截图: