对于对象的创建和数组的简单练习。
package object.test;
import java.util.Random;
/**
*
* @Description 本程序是为了练习数组和类与对象,还有随机数的调用而创建的。
* @author axi Email:2681654638@qq.com
* @version
* @date 2021年2月23日下午9:07:43
*
*/
public class OblectAndArryTest {
public static void bubbleSort(Student stu[]) {//冒泡排序
for(int i = 0;i < stu.length-1;i ++)
{
for(int j = 0;j < stu.length - 1 -i;j++)
{
if(stu[j].score > stu[j+1].score)
{
int temp = stu[j].score;
stu[j].score = stu[j+1].score;
stu[j+1].score = temp;
}
}
}
}
public static void main(String[] args) {
Random rand = new Random();
Student[] stu =new Student[20];
for(int i = 0; i < 20;i++)
{
stu[i] = new Student();
stu[i].number = i+1;
stu[i].state = (int)(Math.random()*6+1);
stu[i].score = (int)(Math.random()*101);
}
bubbleSort(stu);
for(int i = 0;i < 20;i++)
{
System.out.println("编号:"+stu[i].number+" 年级"+stu[i].state+" 分数:"+stu[i].score);
}
}
}
class Student{
int number;
int state;
int score;
}

浙公网安备 33010602011771号