有一个一维数组{7,3,4,9,3,2,6,10,16,12,4},找出最大的值和最小的值并打印输出
package com.homework.lhh;
public class Ex01 {
public static void main(String[] args) {
int[] array = {12,1,2,45,30,50};
int maxIndex = array[0];//定义最大值为该数组的第一个数
int minIndex = array[0];//定义最小值为该数组的第一个数
//遍历循环数组
System.out.print("这个数组为:");
for (int i = 0; i < array.length; i++) {
if(maxIndex < array[i]){
maxIndex = array[i];
}
if(minIndex > array[i]){
minIndex = array[i];
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("这个数组的最大值为:"+maxIndex+"\t最小值为:"+minIndex);
}
}
运行结果如下:
本文介绍了一种在一维数组中查找最大值和最小值的方法,并通过Java代码实现了这一过程。通过对数组元素进行遍历比较,确定数组中的最大值和最小值。

803

被折叠的 条评论
为什么被折叠?



