课堂在线Java程序设计 类的继承 编程题1


学校要进行年终总结,需要对教师和学生的评分结果进行统计。学生的统计数据有三个,教师的统计数据有四个。请你实现一个统计系统,对输入的数据进行整理。 
请你实现一个Person类表示人员,并实现一些必要的方法,再实现Teacher类和Student类,通过类的继承机制完成这个任务。
输入格式:
首先输入一个数字N,表示输入统计的人数。
接下来是N行,每行是用空格隔开的一系列数字。
输出格式:
N行,每行是一个标识符加一个平均得分(向下取整的整数),用空格隔开。
学生的标识符是Student,教师的标识符是Teacher。
输入样例:
2
2 3 4
2 3 4 5
输出样例:
Student 3

Teacher 3


import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int number = Integer.parseInt(in.nextLine());
		String[] str=new String[number];
		for (int i = 0; i < number; i++) {
			str[i] = in.nextLine();
		}
		for (int n = 0; n < number; n++) {
			String[] temp = str[n].split(" ");

			if (temp.length == 3) {
				int[] score = new int[3];
				for (int m = 0; m < 3; m++) {
					score[m] = Integer.parseInt(temp[m]);
				}
				Student student = new Student();
				student.setScore(score);
				System.out.println(student.getIdentity() + " " + student.average());
			}
			if (temp.length == 4) {
				int[] score = new int[4];
				for (int m = 0; m < 4; m++) {
					score[m] = Integer.parseInt(temp[m]);
				}
				Teacher teacher = new Teacher();
				teacher.setScore(score);
				System.out.println(teacher.getIdentity() + " " + teacher.average());
			}
		}
	}

}

class Person {
	final String Identity = "persion";
	int[] score;

	public int[] getScore() {
		return this.score;
	}

	public String getIdentity() {
		return Identity;
	}

	public int average() {
		int sum = 0;
		int avg = 0;
		for (int number = 0; number < this.score.length; number++) {
			sum += this.score[number];
		}
		avg = (int)Math.floor(sum / this.score.length);
		return avg;
	}
}

class Teacher extends Person {
	final String Identity = "Teacher";

	public String getIdentity() {
		return Identity;
	}

	public int[] setScore(int[] score) {
		return this.score = score;
	}

}

class Student extends Person {
	final String Identity = "Student";

	public String getIdentity() {
		return Identity;
	}

	public int[] setScore(int[] score) {
		return this.score = score;
	}

}


只有90分,不知道为啥,有知道的朋友可以留言哦


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值