题解 | #数组遍历# 方法很多
数组遍历
https://www.nowcoder.com/practice/0f8219cb6f6e4e99a1bb0e868e51d60a
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] ary = new int[6]; int max = 0; int min = 0; Scanner scanner = new Scanner(System.in); for (int i = 0; i <ary.length ; i++) { ary[i]=scanner.nextInt(); if( i == 0) { max = ary[i]; min = ary[i]; } else { if(max < ary[i]) { max = ary[i]; } if(min > ary[i]) { min = ary[i]; } } } //write your code here...... max = ary[0]; min = ary[0]; for(int i=0;i<ary.length;i++) { max=Math.max(ary[i],max); min=Math.min(ary[i],min); } Arrays.sort(ary);//先排序->升序 max = ary[ary.length-1];//最后一个最大 min = ary[0];//第一个最小 System.out.println(max+" "+min); } }
遍历获得大小,直接比较;调用Math.max(a,b) Math.min(a,b)
import java.util.Arrays
Arrays.sort(); 排序也可以
方法太多了,都不熟练
Java语法基础 文章被收录于专栏
基础语法的熟悉