Process
1. Define variables, save the elements at index 0 of the array, and traverse the elements.
2. Compare the elements and the variable that holds the 0 index value of the array.
4. If the array element value is greater than the variable value, the variable records the new value. If the array element value is greater than the variable value, the variable records the new value.
Example
package com.itheima.test; import java.util.Scanner; public class Test2Array { /* 需求: 从数组中查找值 int[] arr = {12,45,98,73,60}; 实现步骤: 1. 假设数组中的第一个元素为值 2. 遍历数组, 获取每一个元素, 准备进行比较 3. 如果比较的过程中, 出现了比max更大的, 让max记录更大的值 4. 循环结束后, 打印值. */ public static void main(String[] args) { int[] arr = {12,45,98,73,60}; // 1. 假设数组中的第一个元素为值 int max = arr[0]; // 2. 遍历数组, 获取每一个元素, 准备进行比较 for(int i = 1; i < arr.length; i++){ // 3. 如果比较的过程中, 出现了比max更大的, 让max记录更大的值 if(arr[i] > max){ max = arr[i]; } } // 4. 循环结束后, 打印值. System.out.println("max:" + max); } }
The above is the detailed content of How to find maximum value using Java array. For more information, please follow other related articles on the PHP Chinese website!