数组元素的最大值和最小值
求数组的最大值和最小值经常会碰到,下面给个简单的程序说明一下:
public class OrderDemo{ public static void main(String [] args){ //首先定义一下数组 int score[]={58,79,67,36,96,27}; //接下来定义两个变量,分别代表最大值和最小值 int max=0; int min=0; //将第一个元素的值赋值给max和min max=min=score[0]; //循环数组 for(int x=0;x<score.length;x++){ if(score[x]>max){ max=score[x];//如果大则修改max的内容 } if(score[x]<min){//碰到最小值 min=score[x]; } } System.out.println("最大值:"+max); System.out.println("最小值:"+min);}}