Demo05.java 636 B

123456789101112131415161718192021
  1. package com.loveCoding.j20250517_java_array;
  2. /**
  3. * @author WanJl
  4. * @version 1.0
  5. * @title Demo05
  6. * @description 数组获取最大值:从数组的所有元素中找到最大值
  7. * @create 2025/5/17
  8. */
  9. public class Demo05 {
  10. public static void main(String[] args) {
  11. int[] arr={15,6,4,163,49,8,1,68,49};
  12. int max=0;
  13. for (int i = 0; i < arr.length; i++) { //遍历数组
  14. if(arr[i]>max){ //判断如果一个数组元素大于最大值
  15. max=arr[i];//就把这个元素赋值给max
  16. }
  17. }
  18. System.out.println("数组中最大的元素是:"+max);
  19. }
  20. }