输入n个学生的姓名及其3门功课成绩,要求按输入的逆序逐行输出每个学生的姓名、3门课成绩和平均成绩。若有学生平均成绩低于60分,则不输出该学生信息。使用Java语言
时间: 2024-04-12 11:12:28 浏览: 32
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 学生数量
Student[] students = new Student[n]; // 学生数组
for (int i = 0; i < n; i++) {
String name = scanner.next(); // 学生姓名
int score1 = scanner.nextInt(); // 第一门成绩
int score2 = scanner.nextInt(); // 第二门成绩
int score3 = scanner.nextInt(); // 第三门成绩
Student student = new Student(name, score1, score2, score3); // 创建学生对象
students[i] = student; // 将学生对象存入数组
}
for (int i = n - 1; i >= 0; i--) { // 逆序遍历学生数组
Student student = students[i];
if (student.getAverage() >= 60) { // 判断平均成绩是否大于等于60分
System.out.println(student); // 输出学生信息
}
}
}
}
class Student {
private String name; // 学生姓名
private int score1; // 第一门成绩
private int score2; // 第二门成绩
private int score3; // 第三门成绩
public Student(String name, int score1, int score2, int score3) {
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public double getAverage() { // 计算平均成绩
return (score1 + score2 + score3) / 3.0;
}
@Override
public String toString() { // 重写toString方法
return name + " " + score1 + " " + score2 + " " + score3 + " " + getAverage();
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)