编写程序,实现用户输入5个整数,保存到数组中,然后找到这五个数的最大值.(希望代码和思路都能写上)求数组的最值问题java

解决方案 »

  1.   

    这个没什么难度。
    楼主还是多锻炼下吧。public class Test3 {
    public static void main(String[] args) {
    int[] colum=new int[5];
    Scanner s=new Scanner(System.in);
    for(int i=0;i<5;i++){
    colum[i]=s.nextInt();
    }
    System.out.println(getMax(colum));
    }
    public static int getMax(int[] colum){
    int max=colum[0];
    for(int i:colum){
    if(i>max){
    max=i;
    }
    }
    return max;
    }
    }
      

  2.   


    public class Test{
    public static int getMaxValue(int[] x){
    //bubble sort 
    for(int a=1;a<x.length;a++){
    for(int b=x.length-1;b>=a;b--){
    if(x[b-1]<x[b]){
    int temp = x[b-1] ;
    x[b-1] = x[b];
    x[b] = temp;
    }
    }
    }
    //return max value
    return x[0];
    }

    public static void main(String[] args){
    //if you wanted the array inputed by user,you can use the class of Scaner
    int[] test = {1,24,5,2,3};
    int maxValue = getMaxValue(test);
    System.out.println(maxValue);
    }
    }
      

  3.   

    如果你是想代码简单点的话,直接调用Arrays.sort(arr),然后再输出最后那个就ok了
      

  4.   

    其实commons-lang 包中 ObjectUtils.max(T...values) 方法很好用,支持泛型。