使用Java判断10个整数的最大值和最小值

题目:输入10个整数,判断10个数中的最大值和最小值并输出。要求:使用for循环创建一个长度为10的数组并输出最大值和最小值是第几个数。 

import java.util.Scanner;

public class TheSizeOfThe {

	public static void main(String[] args) {
		int [] ary = new int[10];    //[]表示数组   int ary[] 
		Scanner sc = new Scanner(System.in);//创建一个从键盘输入数据的对象sc
		for (int i = 0;i < 10;i++) {   //三要素
			ary[i] = sc.nextInt();     //循环体
		}
		int max = 	ary[0];   //将数组第一个元素给max,表示最大值
		int min = 	ary[0];
		int zdz=0;
		int zxz=0;
		for(int i = 1;i < 10;i++) {
			if (ary[i] > max) {
				max = ary[i];
				zdz=i+1;
			}
			if (ary[i] < min) {
				min = ary[i];
				zxz=i+1;
		}

	   }
		System.out.println("数组中最大值为:"+max+"。这个最小值位于第"+zdz+"个数");
		System.out.println("数组中最小值为:"+min+"。这个最小值位于第"+zxz+"个数");
	}
}

猜你喜欢

转载自blog.csdn.net/cpm011023/article/details/114436368