Files
javaSE-0113/day05/src/com/inmind/array01/Test06.java

29 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.array01;
/*
获取数组中的最大值
1.定义变量保存数组0索引上的元素
2.遍历数组,获取出数组中的每个元素
3.将遍历到的元素和保存数组0索引上值的变量进行比较
4.如果数组元素的值大于了变量的值,变量记录住新的值
5.数组循环遍历结束,变量保存的就是数组中的最大值
*/
public class Test06 {
//请定义一个长度为5的初始化时固定赋值的整数数组使用遍历获取出数组中最大的值
public static void main(String[] args) {
int[] arr = {-1,-2,3,6,7};
//1.定义变量保存数组0索引上的元素
int max = arr[0];
//2.遍历数组,获取出数组中的每个元素
for (int i = 0; i < arr.length; i++) {
//3.将遍历到的元素和保存数组0索引上值的变量进行比较
int temp = arr[i];//遍历到的每个元素
//4.如果数组元素的值大于了变量的值,变量记录住新的值
if (temp > max) {
max = temp;
}
}
//5.数组循环遍历结束,变量保存的就是数组中的最大值
System.out.println("数组中最大的值:"+max);
}
}