java程序中从键盘输入一组数组并输出,再输出其中最大值和最小值
展开全部
import java.util.Scanner;
public class TestKonw {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入数组长度:");
int n = sc.nextInt();
int[] a = new int[n];
int min = 0,max = 0;
for(int i = 0;i < n;++i){
System.out.println("输入第"+(i+1)+"个数:");
a[i] = sc.nextInt();
if(i==0){
min = a[i];
max = a[i];
}else {
if(min >a[i])
min = a[i];
if(max < a[i])
max = a[i];
}
}
for(int i = 0;i < n;++i)
System.out.print(a[i]+" ");
System.out.println();
System.out.println("最大数是:"+max+" "+"最小数是:"+min);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询