public class ExerTest1 {
public static void main(String[] args) {
//声明student类型的数组
Student1[] stus = new Student1[20];
for(int i = 0;i < stus.length;i++) {
//给数组元素赋值
stus[i] = new Student1();
//给Student对象的属性赋值
stus[i].number = (i + 1);
//年级[1-6];
stus[i].state = (int)(Math.random()*(6-1+1)+1);
//成绩[0-100];
stus[i].score = (int)(Math.random()*(100-0+1));
}
ExerTest1 test = new ExerTest1();
//遍历学生数组
test.print(stus);
System.out.println("*******************");
//问题一:打印三年级(state值为3)的学生信息。
test.searchState(stus, 3);
System.out.println("*********************");
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
test.sort(stus);
//遍历学生数组
test.print(stus);
}
/**
*
* @Description 遍历Student1[]数组的操作
* @author xiong
* @date 2022年3月25日上午11:18:22
* @param stus
*/
public void print(Student1[] stus) {
for(int i = 0;i < stus.length;i++) {
System.out.println(stus[i].info());
}
}
/**
*
* @Description 查找Student数组中指定年级的学生信息
* @author xiong
* @date 2022年3月25日上午11:12:13
* @param stus 要查找的数组
* @param state 要查找的年级
*/
public void searchState(Student1[] stus,int state) {
for(int i = 0;i < stus.length;i++) {
if(stus[i].state == state) {
System.out.println(stus[i].info());
}
}
}
/**
*
* @Description 给Student1数组排序
* @author xiong
* @date 2022年3月25日上午11:15:14
* @param stus
*/
public void sort(Student1[] stus) {
for(int i = 0;i < stus.length - 1;i++) {
for(int j = 0;j < stus.length - 1 - i;j++) {
if(stus[j].score > stus[j + 1].score) {
Student1 temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
}
}
class Student1{
int number;
int state;
int score;
//显示学生信息的方法
public String info
没明白 ExerTest1 test = new ExerTest1();这么操作的含义是什么,我上面stus new对象为什么也在main方法中new,脑海中很混乱,
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- CSDN专家-深度学习进阶 2022-03-27 10:45关注
意思是创建一个 ExerTest1类的对象test
new用来实例化对象,创建内存,放在主函数里本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥100 数字取证课程 关于FAT文件系统的操作
- ¥15 如何使用js实现打印时每页设置统一的标题
- ¥15 安装TIA PortalV15.1报错
- ¥15 能把水桶搬到饮水机的机械设计
- ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
- ¥15 使用小程序wx.createWebAudioContext()开发节拍器
- ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
- ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
- ¥20 用51单片机控制急停。
- ¥15 孟德尔随机化结果不一致