java题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),
答案:2 悬赏:80 手机版
解决时间 2021-02-28 16:26
- 提问者网友:了了无期
- 2021-02-27 16:13
java题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),
最佳答案
- 五星知识达人网友:舍身薄凉客
- 2021-02-27 17:41
import java.util.*;
class Student{
int num;
String name;
double score1;
double score2;
double score3;
public Student(int num,String name,double score1,double score2,double score3){
this.num=num;
this.name=name;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
}
public class Work{
public static void main(String[]args){
Student[] x=new Student[5];
Scanner scanner=new Scanner(System.in);
for(int i=0;i<5;i++){
System.out.println("请输入学生号:");
int num1=scanner.nextInt();
System.out.println("请输入学生姓名:");
String num2=scanner.next();
System.out.println("请输入第一门课的成绩:");
double score1=scanner.nextDouble();
System.out.println("请输入第二门课的成绩:");
double score2=scanner.nextDouble();
System.out.println("请输入第三门课的成绩:");
double score3=scanner.nextDouble();
Student p=new Student(num1,num2, score1,score2,score3);
x[i]=p;
}
System.out.println("第一门课的平均成绩为:"+(x[0].score1+x[1].score1+x[2].score1+x[3].score1+x[4].score1)/5);
System.out.println("第二门课的平均成绩为:"+(x[0].score2+x[1].score2+x[2].score2+x[3].score2+x[4].score2)/5);
System.out.println("第三门课的平均成绩为:"+(x[0].score3+x[1].score3+x[2].score3+x[3].score3+x[4].score3)/5);
}
}
class Student{
int num;
String name;
double score1;
double score2;
double score3;
public Student(int num,String name,double score1,double score2,double score3){
this.num=num;
this.name=name;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
}
public class Work{
public static void main(String[]args){
Student[] x=new Student[5];
Scanner scanner=new Scanner(System.in);
for(int i=0;i<5;i++){
System.out.println("请输入学生号:");
int num1=scanner.nextInt();
System.out.println("请输入学生姓名:");
String num2=scanner.next();
System.out.println("请输入第一门课的成绩:");
double score1=scanner.nextDouble();
System.out.println("请输入第二门课的成绩:");
double score2=scanner.nextDouble();
System.out.println("请输入第三门课的成绩:");
double score3=scanner.nextDouble();
Student p=new Student(num1,num2, score1,score2,score3);
x[i]=p;
}
System.out.println("第一门课的平均成绩为:"+(x[0].score1+x[1].score1+x[2].score1+x[3].score1+x[4].score1)/5);
System.out.println("第二门课的平均成绩为:"+(x[0].score2+x[1].score2+x[2].score2+x[3].score2+x[4].score2)/5);
System.out.println("第三门课的平均成绩为:"+(x[0].score3+x[1].score3+x[2].score3+x[3].score3+x[4].score3)/5);
}
}
全部回答
- 1楼网友:几近狂妄
- 2021-02-27 19:08
public void setJavaLanguage(double javaLanguage) { this.javaLanguage = javaLanguage; } public Course(double mathematics, double english, double javaLanguage) { super(); this.mathematics = mathematics; this.english = english; this.javaLanguage = javaLanguage; } public Course() { super(); }}Student类:public class Student { private String id;// 学生编号 private String name;// 学生姓名 private Course c;// 学生课程分数public class test { public static void main(String[] args) { Student[] stu = new Student[5]; for (int i = 0; i < stu.length; i++) { System.out.println("请输入编号"); Scanner s1 = new Scanner(System.in); String id = s1.next(); System.out.println("请输入姓名"); Scanner s2 = new Scanner(System.in); String name = s2.next(); System.out.println("请输入数学成绩"); Scanner sc1 = new Scanner(System.in); double c1 = sc1.nextDouble(); System.out.println("请输入英语成绩"); Scanner sc2 = new Scanner(System.in); double c2 = sc2.nextDouble(); System.out.println("请输入java成绩"); Scanner sc3 = new Scanner(System.in); double c3 = sc3.nextDouble(); stu[i] = new Student(id, name, new Course(c1,c2,c3)); test t = new test(); t.doIO(stu[i]); } } public void doIO(Student stu){ PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter(new FileWriter("E:\\stud.txt",true))); out.println(stu.getId()+" "+stu.getName()+" 数学:"+stu.getC().getMathematics()+" 英语:"+stu.getC().getEnglish()+" java:"+stu.getC().getJavaLanguage()+" 平均分:"+((stu.getC().getMathematics()+stu.getC().getEnglish()+stu.getC().getJavaLanguage())/3));//这里计算平均分的时候应该要用BigDecimal类的,但是简略了,直接不考虑小数…… } catch (IOException e) { e.printStackTrace(); } finally{ try{ out.close(); }catch (Exception e) { e.printStackTrace(); } } }}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯