编写测试类Test,在main方法中输入5个学生信息,或从文件读入5个学生数据,然后输出每个学生的信息Java

编写一个学生类Student,它具有数据成员:学号【id】、姓名、数学成绩、英语成绩、计算机成绩具有成员函数:求三门课总成绩的函数sum();求三门课平均成绩的函数average();显示学生数据信息的函数display(),输出每个学生的学号、姓名、数学成绩、英语成绩、计算机成绩 、总成绩、平均成绩。

编写测试类Test,在main方法中输入5个学生信息,或从文件读入5个学生数据,然后输出每个学生的信息,格式如下:

学号 姓名 数学成绩 英语成绩 计算机成绩 总成绩 平均成绩。

XX XX NN NN NN NN NN

思考:如何按下列方式显示

id = 2018115321

name = 张三

cj1 = 87

cj2 = 98

cj3 = 99

总成绩 = 284

平均成绩 = 94

id = 2018131136

name = 蒋平平

cj1 = 67

cj2 = 98

cj3 = 99

总成绩 = 264

平均成绩 = 88

id = 2019131318

name = 谢大脚

cj1 = 87

cj2 = 90

cj3 = 99

总成绩 = 276

平均成绩 = 92

。。。

import java.util.*;
import java.io.*;

public class Test {
    public static void main(String[] args) {
        try {
        	Scanner sc = new Scanner(new File("11.txt"));
        	//System.out.println ("学号\t\t 姓名	数学成绩 英语成绩 计算机成绩 总成绩	平均成绩");
        	while (sc.hasNextLine()){
        		String s = sc.nextLine().trim();
        		String[] aa = s.split("\\s+");
        		Student st = new Student(aa[0],aa[1],Integer.parseInt(aa[2]),Integer.parseInt(aa[3]),Integer.parseInt(aa[4]));
        		//st.display();
        	System.out.println (st.toString());
        	}
    
}
catch (Exception ex) {
	System.out.println ("error");
}
    }
}
class Student{
	private String ID,name;
	private int cj1,cj2,cj3;
	
	public int sum(){
		return cj1+cj2+cj3;
	}
	public double average(){
		return (cj1+cj2+cj3)/3.0;
	}
	public void display(){
		String s = ""+ID +"\t"+ name +"\t"+ cj1 +"\t"+ cj2 +"\t"+ cj3 +"\t"+ sum() +"\t"+ average();
		System.out.println (s);
		}


	public Student(String ID, String name, int cj1, int cj2, int cj3) {
		this.ID = ID;
		this.name = name;
		this.cj1 = cj1;
		this.cj2 = cj2;
		this.cj3 = cj3;
	}
	
	public void setID(String ID) {
		this.ID = ID; 
	}

	public void setName(String name) {
		this.name = name; 
	}

	public void setCj1(int cj1) {
		this.cj1 = cj1; 
	}

	public void setCj2(int cj2) {
		this.cj2 = cj2; 
	}

	public void setCj3(int cj3) {
		this.cj3 = cj3; 
	}

	public String getID() {
		return (this.ID); 
	}

	public String getName() {
		return (this.name); 
	}

	public int getCj1() {
		return (this.cj1); 
	}

	public int getCj2() {
		return (this.cj2); 
	}

	public int getCj3() {
		return (this.cj3); 
	}

	public String toString() {

		String sep = System.getProperty("line.separator");//\n

		StringBuffer buffer = new StringBuffer();
		buffer.append(sep);
		buffer.append("ID = ");
		buffer.append(ID);
		buffer.append(sep);
		buffer.append("name = ");
		buffer.append(name);
		buffer.append(sep);
		buffer.append("cj1 = ");
		buffer.append(cj1);
		buffer.append(sep);
		buffer.append("cj2 = ");
		buffer.append(cj2);
		buffer.append(sep);
		buffer.append("cj3 = ");
		buffer.append(cj3);
		buffer.append(sep);
		buffer.append("total=");
		buffer.append(sum());
		buffer.append(sep);
		buffer.append("平均=");
		buffer.append(average());
		
		return buffer.toString();
	}
}

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值