Java的最小和最大数组值 [英] Java Minimum and Maximum values in Array

查看:167
本文介绍了Java的最小和最大数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我code不给错误,但它没有显示的最小和最大值。在code是:

 扫描仪输入=新的扫描仪(System.in);    int数组[] =新INT [10];    的System.out.println(现在输入数字。);           的for(int i = 0; I< array.length,我++){
           接下来INT = input.nextInt();
           // sentineil输入999时将停止循环
           如果(下一== 999)
           打破;
           数组[我] =下一个;
           //获取最大的数
           getMaxValue(数组);
           //获取最小的数
           getMinValue(数组);
    }
    的System.out.println(这是您输入的数字。);
    printArray(数组);}
//获得最大价值
公共静态INT getMaxValue(INT []数组){
      INT包括maxValue =阵列[0];
      的for(int i = 1; I< array.length,我++){
      如果(阵列[I]>包括maxValue){
      包括maxValue =阵列[我]         }
     }
             返回包括maxValue;
}//获取miniumum值
公共静态INT getMinValue(INT []数组){
     INT minValue(最小值)=阵列[0];
     的for(int i = 1; I< array.length,我++){
     如果(阵列[1] - ; minValue(最小值)){
     minValue(最小值)=阵列[我]
        }
     }
    返回minValue(最小值);
}//此方法打印在数组中的元素......
//如果这种情况属实,那么这足以向你证明用户输入已经被//存储在一个数组!!!!!!!
公共静态无效printArray(INT ARR []){    INT N = arr.length;    的for(int i = 0; I< N;我++){
        System.out.print(ARR [I] +);
    }

我需要的System.out.println()来显示呢?还是应该回归工作?我难倒..谢谢大家提前!


解决方案

  getMaxValue(数组);
           //获取最小的数
           getMinValue(数组);

您所呼叫的方法,但是不使用返回的值。

 的System.out.println(getMaxValue(阵列));
的System.out.println(getMinValue(阵列));

My code does not give errors, however it is not displaying the minimum and maximum values. The code is :

Scanner input = new Scanner(System.in);

    int array[] = new int[10];

    System.out.println("Enter the numbers now.");

           for (int i = 0 ; i < array.length; i++ ) {
           int next = input.nextInt();
           // sentineil that will stop loop when 999 is entered
           if (next == 999)
           break;
           array[i] = next;
           // get biggest number
           getMaxValue(array);
           // get smallest number
           getMinValue(array);
    }


    System.out.println("These are the numbers you have entered.");
    printArray(array);

}


// getting the maximum value
public static int getMaxValue(int[] array){  
      int maxValue = array[0];  
      for(int i=1;i < array.length;i++){  
      if(array[i] > maxValue){  
      maxValue = array[i];  

         }  
     }  
             return maxValue;  
}  

// getting the miniumum value
public static int getMinValue(int[] array){  
     int minValue = array[0];  
     for(int i=1;i<array.length;i++){  
     if(array[i] < minValue){  
     minValue = array[i];  
        }  
     }  
    return minValue;  
}  

//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!
public static void printArray(int arr[]){

    int n = arr.length;

    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }

Do i need a system.out.println() to display it? or should the return work? I'm Stumped.. Thank you all in advance!

解决方案

           getMaxValue(array);
           // get smallest number
           getMinValue(array);

You are calling the methods but not using the returned values.

System.out.println(getMaxValue(array));
System.out.println(getMinValue(array)); 

这篇关于Java的最小和最大数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆