在Java数组中找到最小的数字。
示例
以下是必需的程序。
public class Tester {
public static int getSmallest(int[] a) {
int temp;
//对数组排序
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
//返回最小元素
return a[0];
}
public static void main(String args[]) {
int a[] = { 11,10,4, 15, 16, 13, 2 };
System.out.println("Smallest: " + getSmallest(a));
}
}输出结果
Smallest: 2
热门推荐
10 除夕祝福语简短创意牛年
11 分手的祝福语女生简短
12 生日祝福语祝自己简短
13 简短深沉的生日祝福语
14 新年祝福语鼠年简短创意
15 冬至宾馆祝福语简短独特
16 饭桌祝福语长辈文案简短
17 过年祝自己祝福语简短
18 新年英文祝福语简短创意