上机
/学生”类:
² 类名:Student
² 属性:姓名、性别、年龄、学号、5门课程的成绩
² 方法1:在控制台输出各个属性的值、
² 方法2:计算平均成绩
² 方法3:输出各个属性的值和平均成绩
5.2 测试类
创建2个对象,调用方法,要求:对象各个属性的值,从键盘输入。/package test;
import java.util.*;
class teststudent{
Scanner in=new Scanner(System.in);
String name;
String sex;
int age;
int namber;
double []main=new double[5];
public void teststudeny1() {
System.out.println("请输入学生信息:");
name=in.nextLine();
sex=in.nextLine();
age =in.nextInt();
namber=in.nextInt();
}
public void teststudent2() {
for(int i=0;i<main.length;i++) {
System.out.println("请输入学生第"+(i+1)+"个成绩:");
main[i]=in.nextDouble();
}
}
public void teststudent3(){
System.out.println("姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 学号:"+namber);
double average=0;
for(int i=0;i<5;i++){
System.out.println("第"+(i+1)+"成绩为"+main[i]);
average=average+main[1];
}
average=average/5.0;
System.out.println("平均分为:"+average);
}
}
public class student {
public static void main(String[] args) {
teststudent a1=new teststudent();
teststudent a2=new teststudent();
a1.teststudeny1();
a1.teststudent2();
a1.teststudent3();
a2.teststudeny1();
a2.teststudent2();
a2.teststudent3();
}
}