编写一个Java应用程序,实现下列功能:
Ø从键盘输入6个学生的姓名和4门课程(Java程序设计、数据库原理、数据结构和算法设计与分析)成绩保存到数组中。
Ø输入学生姓名查询该学生的各科成绩。
输入科目(或科目代码)查询不及格的人数及名单,并输出该科目的平均分、最高分和最低分。
用java编写一段学生姓名及成绩的查询成绩
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注引自免费微信小程序:皆我百晓生
当然,我可以帮助你编写这个Java应用程序。首先,我们需要创建一个学生类(Student)存储姓名和成绩,然后创建主程序来实现输入、查询等功能。以下是整个程序的详细代码:
import java.util.*; class Student { String name; double[] scores = new double[4]; // 分别对应Java程序设计、数据库原理、数据结构、算法设计与分析四门课的成绩 public Student(String name) { this.name = name; } public void setScore(int subjectIndex, double score) { if (subjectIndex >= 0 && subjectIndex < 4) { scores[subjectIndex] = score; } else { throw new IllegalArgumentException("Invalid subject index"); } } public double getScore(int subjectIndex) { if (subjectIndex >= 0 && subjectIndex < 4) { return scores[subjectIndex]; } else { throw new IllegalArgumentException("Invalid subject index"); } } } public class Main { static final String[] subjects = {"Java程序设计", "数据库原理", "数据结构", "算法设计与分析"}; static List<Student> students = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 输入6个学生的姓名和成绩 for (int i = 0; i < 6; i++) { System.out.println("请输入第" + (i+1) + "个学生的姓名:"); String name = scanner.nextLine(); Student student = new Student(name); for (int j = 0; j < 4; j++) { System.out.println("请输入" + subjects[j] + "课程的成绩:"); double score = scanner.nextDouble(); scanner.nextLine(); // Consume newline left-over student.setScore(j, score); } students.add(student); } // 查询学生姓名及各科成绩 System.out.println("请输入要查询的学生姓名:"); String queryName = scanner.nextLine(); Optional<Student> foundStudent = students.stream() .filter(student -> student.name.equals(queryName)) .findFirst(); if (foundStudent.isPresent()) { Student targetStudent = foundStudent.get(); System.out.println("查询到的学生及其成绩如下:"); for (int j = 0; j < 4; j++) { System.out.println(subjects[j] + ": " + targetStudent.getScore(j)); } } else { System.out.println("未找到该学生的信息!"); } // 查询不及格人数及名单,并输出平均分、最高分和最低分 while (true) { System.out.println("请输入要查询的科目名称或其编号(1-4分别代表四门课程):"); int subjectChoice = scanner.nextInt() - 1; if (subjectChoice >= 0 && subjectChoice < 4) { break; } else { System.out.println("无效的科目选择,请重新输入!"); } } double totalScore = 0.0; double minScore = Double.MAX_VALUE; double maxScore = Double.MIN_VALUE; int failCount = 0; List<String> failedStudents = new ArrayList<>(); for (Student student : students) { double score = student.getScore(subjectChoice); totalScore += score; minScore = Math.min(minScore, score); maxScore = Math.max(maxScore, score); if (score < 60) { // 假设及格线为60分 failCount++; failedStudents.add(student.name); } } double averageScore = totalScore / students.size(); System.out.println("该科目平均分为:" + averageScore); System.out.println("该科目最高分为:" + maxScore); System.out.println("该科目最低分为:" + minScore); System.out.println("不及格人数为:" + failCount); System.out.println("不及格学生名单为:" + failedStudents.toString().replace("[", "").replace("]", "")); } }这段代码首先定义了一个
Student类用于存储每个学生的姓名和成绩,然后在Main类中实现了从键盘输入学生信息并进行查询的功能。同时,对于输入科目查询不及格人数及名单的部分,也进行了相应的逻辑处理。请根据实际需求调整及格分数线和其他细节。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录