用java定义类Student,包含三个属性:学号number(int),年级state(int).成绩score(int)。创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。问题一:打印出3年级(state值为3)的学生信息。 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
时间: 2024-03-24 18:40:32 浏览: 107
好的,我会为您解答这两个问题。
问题一:打印出3年级(state值为3)的学生信息。
```java
import java.util.Random;
public class Student {
private int number;
private int state;
private int score;
public Student(int number, int state, int score) {
this.number = number;
this.state = state;
this.score = score;
}
public int getNumber() {
return number;
}
public int getState() {
return state;
}
public int getScore() {
return score;
}
public static void main(String[] args) {
// 创建20个学生对象
Student[] students = new Student[20];
Random random = new Random();
for (int i = 0; i < 20; i++) {
students[i] = new Student(i + 1, random.nextInt(6) + 1, random.nextInt(101));
}
// 打印出3年级的学生信息
System.out.println("3年级的学生信息如下:");
for (int i = 0; i < 20; i++) {
if (students[i].getState() == 3) {
System.out.println("学号:" + students[i].getNumber() + ",年级:" + students[i].getState() + ",成绩:" + students[i].getScore());
}
}
}
}
```
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
```java
import java.util.Random;
public class Student {
private int number;
private int state;
private int score;
public Student(int number, int state, int score) {
this.number = number;
this.state = state;
this.score = score;
}
public int getNumber() {
return number;
}
public int getState() {
return state;
}
public int getScore() {
return score;
}
public static void main(String[] args) {
// 创建20个学生对象
Student[] students = new Student[20];
Random random = new Random();
for (int i = 0; i < 20; i++) {
students[i] = new Student(i + 1, random.nextInt(6) + 1, random.nextInt(101));
}
// 使用冒泡排序按学生成绩排序
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19 - i; j++) {
if (students[j].getScore() > students[j + 1].getScore()) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 遍历所有学生信息
System.out.println("所有学生信息如下:");
for (int i = 0; i < 20; i++) {
System.out.println("学号:" + students[i].getNumber() + ",年级:" + students[i].getState() + ",成绩:" + students[i].getScore());
}
}
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)