day05--获取数组中的最大值
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.inmind.array01;
|
||||
//数组的操作_获取数组中的最大
|
||||
public class Test06 {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {11,2,345,55,88,99,543};
|
||||
|
||||
//获取数组中每个元素,依次比较,如果大,就记录,循环结束,那么记录的就是最大值
|
||||
//定义一个最大值的变量,用来记录比较中,较大的值
|
||||
int max = arr[0];//假设第一个空间的值最大
|
||||
//遍历比较
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int temp = arr[i];//当前遍历得到的数组中的元素之一
|
||||
if (temp < max) {
|
||||
max = temp;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("当前最小值:"+max);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user