day05-数组的操作_获取数组中的最大值
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.array01;
|
||||||
|
//数组的操作_获取数组中的最大值
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义一个数组
|
||||||
|
int[] arr = {100, 120, 30, 404, 150};
|
||||||
|
//数组中的最大值:直接与数组中每个元素比较,谁大,就记录谁
|
||||||
|
//定义一个最大值
|
||||||
|
int max = arr[0];
|
||||||
|
//遍历数组,依次判断谁大
|
||||||
|
for (int i = 1; i < arr.length; i++) {
|
||||||
|
//获取当前的元素值
|
||||||
|
int current = arr[i];
|
||||||
|
if (current > max) {
|
||||||
|
max = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("数组中最大的值为:"+max);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user