从键盘上输入任意个数的整数,输出最大值和最小值。
package com.szd.test;
import java.util.Scanner;
public class MaxAndMin {
public static void main(String[] args) {
int min = 0;
int max = 0;
Scanner sc = new Scanner(System.in);
System.out.println("输入个数:");
int a = sc.nextInt();
//用数组来存数据
int num[] = new int[a];
for (int i = 0; i < num.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数:");
num[i] = sc.nextInt();
}
/**
* 遍历数组中的数据
*/
System.out.println("输入的数为:");
for (int i : num) {
System.out.print(i + " ");
}
System.out.println();
/**
* 比较大小
*/
for (int i = 0; i < num.length - 1; i++) {
if (num[i] > num[i + 1]) {
int temp = 0;
temp = num[i + 1];
num[i + 1] = num[i];
num[i] = temp;
min = num[i];
max = num[i + 1];
} else {
min = num[i];
max = num[i + 1];
}
}
System.out.println("输入的数中最小值为:" + min);
System.out.println("输入的数中最大值为:" + max);
}
}