分为两个类编写。第一个类:
package study;
class Student{
// 由键盘输入3个学生成绩(国文、英文、数学),并算出其平均
private int id ;
private String name ;
private int gy ;
private int wy ;
private int sx ;
public Student(int id,String name,int gy,int wy,int sx) {
this.id=id;
this.name = name;
this.gy = gy;
this.wy = wy;
this.sx = sx;
}
public String toString(){
return "学生ID:"+this.id+"姓名:" + this.name + ";国语成绩:"
+ this.gy+";外语成绩:"+ this.wy+";数学成绩:"+ this.sx+
";平均成绩:"+ (this.gy+ this.wy+ this.sx )/3;
}
}
第二个类:package study;
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args)throws Exception {
Scanner s = new Scanner(System.in);
Student[] stu = new Student[3];
for (int i = 0; i < stu.length; i++) {
System.out.print("请输入第"+(i+1)+"个学生的ID:");
int id=s.nextInt();
System.out.print("请输入第"+(i+1)+"个学生的姓名:");
String name=s.next();
System.out.print("请输入第"+(i+1)+"个学生的国语成绩:");
int gy=s.nextInt();
System.out.print("请输入第"+(i+1)+"个学生的外语成绩:");
int wy=s.nextInt();
System.out.print("请输入第"+(i+1)+"个学生的数学成绩:");
int sx=s.nextInt();
stu[i]=new Student(id, name, gy, wy, sx);
}
PrintStream ps = null ;// 声明打印流对象
ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "stud.txt"))) ;
for(int i = 0; i < stu.length; i++){
ps.println(stu[i]);
}
}
}
亲测可行,生成文件在D盘的stud.txt文件中,最近正在学习,可以互相交流,支付宝就免了
追问要求平均分的 这个好像没求
追答求了,你自己运行一下,在toString方法中求的,直接写入文件中
打印形式给你复制了一个:
学生ID:123456姓名:张三;国语成绩:80;外语成绩:70;数学成绩:80;平均成绩:76
对了,你有五个学生,把 Student[] stu = new Student[3];中的3改成5就好了
追问搞了半天 我都不知道怎么运行 两个类 我不是这个专业的 只是为了应付作业 大神 求帮忙!
追答留下联系方式,可以指导你,你也可以找学长
追问企鹅770753549