JAVA SE 问题 通过键盘循环录入学生姓名和5门课成绩,计算平均分
- 提问者网友:人生佛魔见
- 2021-04-27 10:54
- 五星知识达人网友:低音帝王
- 2021-04-27 11:35
import java.util.ArrayList;
import java.util.Collections;
public class Test{
public static void main(String[] args) throws IOException
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您的姓名:");
String name = bf.readLine();
System.out.println("请输入您的年龄:");
String age = bf.readLine();
System.out.println("请输入您的性别:");
String sex = bf.readLine();
System.out.println("请输入您的学校:");
String school = bf.readLine();
System.out.println("请输入您的英语成绩:");
String englishScore = bf.readLine();
System.out.println("请输入您的数学成绩:");
String mathScore = bf.readLine();
System.out.println("请输入您的语文成绩:");
String chineseScore = bf.readLine();
System.out.println("请输入您的电脑成绩:");
String computerScore = bf.readLine();
System.out.println("学生信息表");
System.out.println("|-姓名"+name);
System.out.println("|-年龄"+age);
System.out.println("|-性别"+sex);
System.out.println("|-学校"+school);
System.out.println("┇--英语成绩:"+englishScore);
System.out.println("┇--数学成绩:"+mathScore);
System.out.println("┇--语文成绩:"+chineseScore);
System.out.println("┇--电脑成绩:"+computerScore);
ArrayList list = new ArrayList();
list.add(new Integer(Integer.parseInt(englishScore)));
list.add(new Integer(Integer.parseInt(mathScore)));
list.add(new Integer(Integer.parseInt(chineseScore)));
list.add(new Integer(Integer.parseInt(computerScore)));
Collections.sort(list);
int allScore = Integer.parseInt(englishScore)+Integer.parseInt(mathScore)+Integer.parseInt(chineseScore)+Integer.parseInt(computerScore);
System.out.println("┇--总成绩:"+allScore);
System.out.println("┇--平均成绩:"+allScore/4);
System.out.println("┇--最高成绩:"+list.get(3));
System.out.println("┇--最低成绩:"+list.get(0));
}
}
- 1楼网友:西岸风
- 2021-04-27 13:11
- 2楼网友:往事隔山水
- 2021-04-27 12:00
可以专门的定义一个学生类
class Student
{
ArrayList a = new ArrayList ;
private int num;//学号 private String name;//姓名 private int[] score = new int[5];//5课成绩 private int avg;//平均分
Student(){}
// 通过输入学生的学号和姓名,再输入学生的成绩 Student(int num, String name) throws IOException {
this.num = num; this.name = name;
int sum = 0;
System.out.println("请输入该同学的5科成绩"); for (int i = 0; i < score.length; i++) { score[i] = sc.nextInt(); sum += score[i]; } avg = (int) sum / score.length;
}
public String toString()//为了输出学生信息
{
public String toString() { String information = num + " " + name + " " + score[0] + " " + score[1] + " " + score[2] + " " + score[3]+ " " + score[4]; return information;
}
// 单个学生的输入信息 public void input() throws IOException { System.out.println("请输入学生的信息"); System.out.println(" 学号" + " " + " 姓名"); int num = sc.nextInt(); String name = sc.next();
a.add(new Student(num, name));// 放置每个学生 }
public static vodi main(String[] args)
{
Student a = new Student();
for(int i=0;i<5;i++)
{
a.input();
}
}
}
}
这个代码是我以前做过的一的小项目中的截取片段,思路是,将学生的信息,单独存放在Student类中,在类中增加一个 加入学生信息的方法,在主方法中去调用就可以了,这样想输几个就输几个
- 3楼网友:佘樂
- 2021-04-27 11:48
很不错,没有任何问题,也想不出该怎么再简单化了