Java基础(数组)找最大值

//对象数组
public class StudentTest {
    public static void main(String[] args) {
        Student[] stuArr = new Student[15];
        String[] names1 = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
                "毋丘", "贺兰", "綦毋", "屋庐", "独孤", "南郭", "北宫", "王孙"};
        String[] names2 = {"娟", "英", "皋华", "慧", "巧", "美", "静", "晋娜", "翠", "淑", "漆红", "双惠", "竹雅", "珠", "芝", "玉", "萍", "娥", "玲", "挚芬", "芳", "娜", "彩", "云宾", "环", "文惠", "雅", "珠", "春"};
        for (int i = 0; i < stuArr.length; i++) {
            int id = i + 1;
            int index1 = (int) (Math.random() * names1.length);
            int index2 = (int) (Math.random() * names2.length);
            String name = names1[index1] + names2[index2];
            int grade = (int) (Math.random() * 6 + 1);
            double score = (int) (Math.random() * 101);
            stuArr[i] = new Student(id, name, grade, score);
        }
        //遍历
        for (Student tmp :
                stuArr) {
            System.out.println(tmp.toString());
        }
        System.out.println("==========================================================================");
        //找出全校最高分
        Student maxStudent = stuArr[0];//声明变量保存元素值
        for (Student tmp :
                stuArr) {
            if (tmp.getScore() > maxStudent.getScore()) {
                maxStudent = tmp;
            }
        }
        System.out.println("最高分" + maxStudent.toString());
        //下标方式找出最高分
        int maxIndex = 0;
        for (int i = 0; i < stuArr.length; i++) {
            if (stuArr[i].getScore() > stuArr[maxIndex].getScore()) {//如果i位置的元素的分数大于maxIndex位置的学生的分数
                maxIndex = i;//交换位置
            }
        }
        System.out.println("最高分" + stuArr[maxIndex].toString());
        //找出三年级最高分 下标方法
        int maxIndex3 = -1;
        for (int i = 0; i < stuArr.length; i++) {
            if (stuArr[i].getGrade() == 3) {//判断年级
                if (maxIndex3 == -1) {//找到第一个3年级 无条件刷入
                    maxIndex3 = i;
                } else if (stuArr[maxIndex3].getScore() < stuArr[i].getScore()) {//比大小
                    maxIndex3 = i;
                }
            }
        }
        if (maxIndex3 == -1) {//判断maxIndex是否被刷过未被刷过则没有三年级
            System.out.println("没有三年级学生");
        } else {
            System.out.println("三年级最高分" + stuArr[maxIndex3]);
        }
    }
}
//基本数组
public class ArrayTest1 {
    public static void main(String[] args) {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 20);
        }
        for (int tmp : arr) {
            System.out.print(tmp + " ");
        }
        System.out.println();
        //数字方式找最大值
        int max = arr[0];
        for (int tmp :
                arr) {
            if (tmp > max) {
                max = tmp;
            }
        }
        System.out.println("max" + max);
        //下标方式找最大值
        int maxIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > arr[maxIndex]) {//如果i”位置“的元素的值大于maxIndex”位置“元素的值
                maxIndex = i;
            }
        }
        System.out.println("max" + arr[maxIndex]);
        //找能被7整除的数的最大值,用下标的方式 有条件找
        int maxIndex7 = -1;//初始设置为-1
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 7 == 0) {
                if (maxIndex7 == -1) {//找到第一个能被7整除的数 无条件刷入
                    maxIndex7 = i;
                } else if (arr[i] > arr[maxIndex7]) {//刷入后再进行比大小
                    maxIndex7 = i;
                }
            }
        }
        if (maxIndex7 == -1) {
            System.out.println("没有能被7整除的数");
        } else {
            System.out.println("能被7整除max" + arr[maxIndex7]);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值