Use Java to determine the maximum and minimum values of 10 integers

Question: Input 10 integers, judge the maximum and minimum of the 10 numbers and output. Requirements: Use a for loop to create an array of length 10 and output the number of the maximum and minimum values. 

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+"个数");
	}
}

 

Guess you like

Origin blog.csdn.net/cpm011023/article/details/114436368