1、 创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),求其总人数和平均分。(学生对象不得少于5个,数据来源键盘输入)
时间: 2024-01-04 22:02:20 浏览: 22
以下是使用Java语言实现的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[5];
// 输入学生信息
for (int i = 0; i < students.length; i++) {
System.out.println("请输入第" + (i+1) + "个学生的学号、姓名和成绩(用空格隔开):");
int id = scanner.nextInt();
String name = scanner.next();
double score = scanner.nextDouble();
students[i] = new Student(id, name, score);
}
// 计算总人数和平均分
int total = students.length;
double sum = 0.0;
for (Student student : students) {
sum += student.getScore();
}
double average = sum / total;
System.out.println("总人数为:" + total);
System.out.println("平均分为:" + average);
}
}
class Student {
private int id;
private String name;
private double score;
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
```
运行程序后,依次输入5个学生的学号、姓名和成绩,程序会输出总人数和平均分。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)