完整代码:
public class {
public static void main(String[] args) {
//创建20个学生对象:
Student[] std = new Student[20];
for(int i = 0;i < std.length;i++) {
std[i] = new Student();
//学号为1~20:
std[i].number = (i+1);
//年级Math.random[0.0 1.0)*6-->[0.0 6.0)-->(int)[0 6)+1-->[1 6]
std[i].state = (int)(Math.random()*6) + 1;
//成绩[0 100]
std[i].score = (int)(Math.random()*100) + 1;
}
}
//定义类Student:
class Student{
//包含三个属性:
int number;//学号
int state;//年级
int score;//成绩
//遍历学生属性信息方法:
public String info() {
return "学号:"+number+"\t"+"年级:"+state+"\t"+"成绩:"+score;
}
//遍历20个学生的属性信息:
public void arrayergo(Student[] std) {
for(int i = 0;i < std.length;i++){
System.out.println(std[i].info());
}
}
//问题一:打印出3年级(state值为3)的学生信息:
public void statefind(Student[] std,int state) {
for(int i = 0;i < std.length;i++) {
if(std[i].state==state) {
System.out.println(std[i].info());
}
}
}
}
}
图片:
![img](
https://img-mid.csdnimg.cn/release/static/image/mid/ask/939358552626159.png)