如何检查数组中的所有值是否高于特定数量?

时间:2022-12-08 07:59:20

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

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

极限变量是数组中所有数都必须为grater或qual的数。

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

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

3 个解决方案

#1


3  

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

不要检查所有的元素是否大于一个特定的数字,只需检查一个数字是否小于这个特定的数字,如果还有一个返回true,则返回false

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

#2


1  

Unless you are passing in the value for the limit as well as the array you're checking, the method won't work. Change your header to something like:

除非您为限制和正在检查的数组传递值,否则该方法将无法工作。把你的标题改成:

public static boolean isGameover(int limit, int[] data)

This should be enough to get your code working (your parameters aren't set up properly). I offer you an alternate solution with less steps involved.

这应该足以让您的代码正常工作(您的参数设置不正确)。我给你提供了一种不需要太多步骤的替代方案。

You need to find out if all of the numbers in the array are greater than the limit, so the only condition you need to check is if any of the elements of the array are less than the limit.

您需要查明数组中的所有数字是否都大于极限,因此您需要检查的唯一条件是数组中的任何元素是否小于极限。

for(int i = 0; i < data.length; i++){
    if(data[i] <= limit) // the condition you're checking for is that they're all greater than, so if its less than or equal to, it gets flagged
        return false;
}
return true; // if it goes through the whole array without triggering a false, it has to be true.

#3


0  

You can do it easily by using ES 6 new feature called every method and Arrow function

你可以很容易地使用ES 6的新特性,称为每个方法和箭头函数。

var bAges= [11, 22, 4, 5, 6, 7, 8, 99];

var gAges= [11, 22, 43, 45, 16, 71, 88, 99];

var ageLimit = 10 ;

 bAges.every(x => x >= ageLimit ); //  false

gAges.every(x => x >= ageLimit ); // true

Explanation:

解释:

every method checks all the array item is greater then or equal 10 if all array items are greater or equal to then it will return true else it will return false. In Es 6 there was another method called some it will check all the item if one item matches the condition then it will return true else it will return false

每个方法检查所有数组项大于或等于10如果所有数组项大于或等于,它将返回true否则返回false。在Es 6中有另一种方法叫一些它会检查所有的项如果一个项与条件匹配那么它将返回true否则它将返回false。

 bAges.some(x => x >= ageLimit ); //  ture

[2, 3, 4, 5, 6 ].some(x => x >= ageLimit ); // false

#1


3  

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

不要检查所有的元素是否大于一个特定的数字,只需检查一个数字是否小于这个特定的数字,如果还有一个返回true,则返回false

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

#2


1  

Unless you are passing in the value for the limit as well as the array you're checking, the method won't work. Change your header to something like:

除非您为限制和正在检查的数组传递值,否则该方法将无法工作。把你的标题改成:

public static boolean isGameover(int limit, int[] data)

This should be enough to get your code working (your parameters aren't set up properly). I offer you an alternate solution with less steps involved.

这应该足以让您的代码正常工作(您的参数设置不正确)。我给你提供了一种不需要太多步骤的替代方案。

You need to find out if all of the numbers in the array are greater than the limit, so the only condition you need to check is if any of the elements of the array are less than the limit.

您需要查明数组中的所有数字是否都大于极限,因此您需要检查的唯一条件是数组中的任何元素是否小于极限。

for(int i = 0; i < data.length; i++){
    if(data[i] <= limit) // the condition you're checking for is that they're all greater than, so if its less than or equal to, it gets flagged
        return false;
}
return true; // if it goes through the whole array without triggering a false, it has to be true.

#3


0  

You can do it easily by using ES 6 new feature called every method and Arrow function

你可以很容易地使用ES 6的新特性,称为每个方法和箭头函数。

var bAges= [11, 22, 4, 5, 6, 7, 8, 99];

var gAges= [11, 22, 43, 45, 16, 71, 88, 99];

var ageLimit = 10 ;

 bAges.every(x => x >= ageLimit ); //  false

gAges.every(x => x >= ageLimit ); // true

Explanation:

解释:

every method checks all the array item is greater then or equal 10 if all array items are greater or equal to then it will return true else it will return false. In Es 6 there was another method called some it will check all the item if one item matches the condition then it will return true else it will return false

每个方法检查所有数组项大于或等于10如果所有数组项大于或等于,它将返回true否则返回false。在Es 6中有另一种方法叫一些它会检查所有的项如果一个项与条件匹配那么它将返回true否则它将返回false。

 bAges.some(x => x >= ageLimit ); //  ture

[2, 3, 4, 5, 6 ].some(x => x >= ageLimit ); // false