Java程序-查找出数组中的最小值
时间:2020-02-23 14:35:08 来源:igfitidea点击:
在本教程中,Java程序在数组中找到最小值。
这是一个简单的算法,可以找到数组中的最小值。
- 使用ARR [0]初始化SML,例如:数组中的第一个元素。
- 如果当前元素小于SML,则将SML设置为当前元素。
import java.util.Scanner;
public class Finder
{
public static void main(String args[])
{
int sml, size, i;
int numArr[] = new int[50];
Scanner scan = new Scanner(System.in);
System.out.print("Enter array Size : ");
size = scan.nextInt();
System.out.print("Enter array elements : ");
for(i=0; i<size; i++){
numArr[i] = scan.nextInt();
}
System.out.print("Searching for the Smallest Element....\n\n");
sml = numArr[0];
for(i=0; i<size; i++){
if(sml > numArr[i]){
sml = numArr[i];
}
}
System.out.print("Smallest Element = " + sml);
}
}
输出:
Enter array Size : 3 Enter array elements : 45 76 35 Searching for the Smallest Element....Smallest Element = 35

