题目:
输出20名学生的学号、年级、成绩,学号为1-20,年级为1-6,成绩为0-100,学号和成绩用随机数;
1.输出所有学生的信息
2.输出年级为3的学生信息
public class StudentTest {
public static void main(String[ ] arges){
//声明Student类型的对象数组
Student[ ] stus = new Student[20];
for(int i = 0; i < stus.length; i++){
//给数组元素赋值
stus[i] = new Student();
//给Student对象的属性赋值
stus[i].number = i + 1;
stus[i].grade = (int)(Math.random() * (6 - 1 + 1) +1);
stus[i].score = (int)(Math.random() * (100 - 0 + 1));
}
//遍历学生数组
for(int i = 0; i < stus.length; i++){
System.out.println(stus[i]);//输出为对象地址值
System.out.println(stus[i].info());
//System.out.println(stus[i].number + ',' + stus[i].grade + ',' + stus[i].score);
}
//打印出年级为3的学生信息
for(int i = 0; i < stus.length; i++){
if(stus[i].grade == 3){
System.out.println(stus[i].info());
}
}
}
}
class Student{
//声明学号、年级、成绩
int number;
int grade;
int score;
//输出学生信息
public String info(){
return "学号:" + number + ",年级:" + grade + ",成绩:" + score;
}
}