使用Java的数组求五个学生的平均分,学生的分数从键盘获得
时间: 2023-12-14 21:37:11 浏览: 34
可以使用Java的Scanner和数组来实现这个功能。具体实现如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double[] scores = new double[5];
double sum = 0;
for (int i = 0; i < 5; i++) {
System.out.print("请输入第" + (i+1) + "个学生的分数:");
scores[i] = scanner.nextDouble();
sum += scores[i];
}
double avg = sum / 5;
System.out.println("五个学生的平均分是:" + avg);
}
}
```
运行程序后,依次输入五个学生的分数,程序将计算出它们的平均分并输出。
注:该程序中使用了Scanner类来读取用户输入的数据,并使用了一个double类型的数组来存储五个学生的分数。在输入分数的同时,累加器变量sum也会不断地增加,最后通过sum除以5求得平均分并输出。
相关问题
java 从键盘输入10个学生成绩,统计各个分数段的比例
可以的,这个问题我可以回答。统计各个分数段的比例,可以使用数组来存储学生成绩,然后遍历数组,统计各个分数段的数量,最后计算比例即可。以下是一个示例代码:
```java
import java.util.Scanner;
public class ScoreAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] scores = new int[10];
int[] counts = new int[6]; // 分数段数量,从0-59、60-69、70-79、80-89、90-99、100分六个段
System.out.println("请输入10个学生成绩:");
for (int i = 0; i < 10; i++) {
scores[i] = scanner.nextInt();
if (scores[i] < 0 || scores[i] > 100) {
System.out.println("请输入0-100之间的数字!");
i--;
} else if (scores[i] < 60) {
counts[0]++;
} else if (scores[i] < 70) {
counts[1]++;
} else if (scores[i] < 80) {
counts[2]++;
} else if (scores[i] < 90) {
counts[3]++;
} else if (scores[i] < 100) {
counts[4]++;
} else {
counts[5]++;
}
}
System.out.println("0-59分段的人数占比:" + (double)counts[0]/10);
System.out.println("60-69分段的人数占比:" + (double)counts[1]/10);
System.out.println("70-79分段的人数占比:" + (double)counts[2]/10);
System.out.println("80-89分段的人数占比:" + (double)counts[3]/10);
System.out.println("90-99分段的人数占比:" + (double)counts[4]/10);
System.out.println("100分的人数占比:" + (double)counts[5]/10);
}
}
```
希望以上代码可以帮到您。
1、 创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),求其总人数和平均分。(学生对象不得少于5个)要求键盘输入
以下是一个示例程序,可以创建一个学生对象数组,从键盘输入学生信息,计算总人数和平均分:
```java
import java.util.Scanner;
public class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生人数:");
int n = scanner.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的信息:");
System.out.print("学号:");
int id = scanner.nextInt();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("成绩:");
int score = scanner.nextInt();
students[i] = new Student(id, name, score);
}
int totalScore = 0;
for (int i = 0; i < n; i++) {
totalScore += students[i].getScore();
}
double averageScore = (double) totalScore / n;
System.out.println("总人数:" + n);
System.out.println("平均分:" + averageScore);
}
}
```
在这个示例程序中,我们定义了一个 `Student` 类,包含学生的三个属性:学号、姓名和成绩。然后,我们从键盘输入学生人数,创建一个学生对象数组,从键盘输入每个学生的信息,然后遍历数组,计算总分数和平均分数。最后输出结果。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)