如何检查数组中的所有值是否都超过特定值? [英] How to check if all values in an array are higher than a specific amount?

查看:115
本文介绍了如何检查数组中的所有值是否都超过特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以在此程序中,我要检查数组中的值 all 是否大于特定数字。

OK so in this program I'm making I have to check if all of the values in the Array are greater than a specific number.

这是我到目前为止所拥有的

this is what i have so far

public static boolean isGameOver(){
  int check = 0;
  for(int k = 0; k < data.length; k++){
    if (data[k] >= limit)
      check++;
  }
  if (check == data.length)
    return true;
  else 
    return false;
}

limit变量是数组中所有数字必须为

the limit variable is the number that all the number in the array have to be grater or qual to.

您能帮我解决这个问题,并解释为什么我的方法行不通吗?

Can you help me fix this and explain why my way doesn't work?

推荐答案

不是检查所有元素是否都大于特定数字,而是要检查一个数字是否小于特定数字,然后返回false 如果还有其他 return true

Instead of checking if all elements are greater than a specific number just check if one number is less than the specific number and return false if there is one else return true

public static boolean isGameOver(int limit, int[] data){
      for(int k = 0; k < data.length; k++){
        if (data[k] < limit)
          return false;
      }
      return true;
    }

这篇关于如何检查数组中的所有值是否都超过特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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