第三次上机
Posted susususu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三次上机相关的知识,希望对你有一定的参考价值。
学生”类:
2 类名:Student
2 属性:姓名、性别、年龄、学号、5门课程的成绩
2 方法1:在控制台输出各个属性的值、
2 方法2:计算平均成绩
2 方法3:输出各个属性的值和平均成绩
5.2 测试类
创建2个对象,调用方法,要求:对象各个属性的值,从键盘输入。
package test;
public class Student {
private String name;
private char sex;
private int age;
private String number;
private double[] score = new double[5];
public void setname(String name) {
this.name = name;
}
public String getname() {
return name;
}
public void setsex(char sex) {
this.sex = sex;
}
public char getsex() {
return sex;
}
public void setage(int age) {
this.age = age;
}
public int getage() {
return age;
}
public void setnumber(String number) {
this.number = number;
}
public String getnumber() {
return number;
}
public void setscore(double score[]) {
this.score = score;
}
public double[] getscore() {
return score;
}
public double getaverage(double score[]) {
double average=0;
for(double k:score) {
average = average+k;
}
return average/5.0;
}
public void Printinformation(double score[]) {
System.out.println("姓名:"+getname()+" 性别:"+getsex()+" 学号:"+getnumber()+" 年龄"+getage());
System.out.println("5门课的成绩;");
for(double s:score)
System.out.println(s);
System.out.println("平均成绩:"+getaverage(score));
}
}
import java.util.Scanner;
public class Informance {
public static void main(String[] args) {
double[] scores1 = new double[5];
double[] scores2 = new double[5];
Scanner sc = new Scanner(System.in);
Student stu1 = new Student();
Student stu2 = new Student();
System.out.println("请输入第一个学生的信息:");
stu1.setname(sc.next());
stu1.setage(sc.nextInt());
stu1.setnumber(sc.next());
stu1.setsex(sc.next().charAt(0));
System.out.println("请输入5门课的成绩:");
for(int i = 0;i<scores1.length;i++) {//注意:for-each语句只能用遍历数组,不能给数组赋值
scores1[i] = sc.nextDouble();
}
stu1.setscore(scores1);
System.out.println("请输入第二个学生的信息:");
stu2.setname(sc.next());
stu2.setage(sc.nextInt());
stu2.setnumber(sc.next());
stu2.setsex(sc.next().charAt(0));
System.out.println("请输入5门课的成绩:");
for(int i = 0;i<scores2.length;i++) {
scores2[i] = sc.nextDouble();
}
stu2.setscore(scores2);
System.out.println("第一个学生的信息:");
stu1.Printinformation(scores1);
System.out.println("第二个学生的信息:");
stu2.Printinformation(scores2);
}
}
以上是关于第三次上机的主要内容,如果未能解决你的问题,请参考以下文章