如何找到最大值和最小值 [英] How to find max and min value

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

问题描述

那么如何找到数字组的最大值和最小值。例如:数字是

So how do I find max and min value of group of numbers. Ex: The numbers are

int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,};

接下来是如何找出2出现在阵列中的次数。
这就是我的想法。

The next things is how to find out how many times 2 appears in the array. This is what I think.

char a = "2"
int count = 0;
if (num.length = a) {
    count++;
    System.out.print (count);
}


推荐答案


如何查找数字组的最大值和最小值。

How do I find max and min value of group of numbers.



int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,};
getMaxValue(num);
getMinValue(num);

// 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;  
}  

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

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