Java 如何在数组中查找大于、小于或等于某个值的数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26558397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find numbers in an array that are greater than, less than, or equal to a value?
提问by ChristinaDoyle
I am trying to output the values that are less than five and the values that are greater than the average of all the values in the array . I can't figure out how to make it all work out and output the correct numbers. Can anyone help?
我试图输出小于 5 的值和大于数组中所有值的平均值的值。我不知道如何让它全部解决并输出正确的数字。任何人都可以帮忙吗?
Here's what I have, I am almost there I just don't know what I'm doing wrong.
这就是我所拥有的,我快到了,我只是不知道我做错了什么。
{
int[] numbers = {2, 4, 6, 8, 10, 12, 14, 16};
int sum = 0;
for (int x : numbers)
sum += x;
System.out.println("Numbers in order:");
{
for(int x = 0; x < numbers.length; ++x)
System.out.println(numbers[x]);
}
System.out.println("Numbers in reverse order:");
{
for(int x = 0; x < numbers.length; ++x)
System.out.println(numbers[x]);
}
System.out.println("The sum of all eight numbers is: " + sum);
System.out.println("The lowest number is: " + numbers[0]);
System.out.println("The highest number is: " + numbers[7]);
System.out.println("The average of all eight numbers is: " + sum * 1.0 / 8);
System.out.println("All numbers higher than the average are: ");
{
for(int x = 0; x < numbers.length && x > 9;)
{numbers[x] = x + 1;}
int x = 0;
System.out.println(numbers[x]);
}
System.out.println("Numbers less than 5: ");
{
for(int x = 0; x < numbers.length && x > 5;)
{numbers[x] = x + 1;}
int x = 0;
System.out.println(numbers[x]);
}
}
采纳答案by kjosh
I assume this is a homework question so I won't give you a straight code answer but try to help anyways.
我认为这是一个家庭作业问题,所以我不会给你一个直接的代码答案,但无论如何都会尝试提供帮助。
System.out.println("Numbers less than 5: ");
for(int x = 0; x < numbers.length && x > 5;) {
numbers[x] = x + 1;
}
The counting for-loop statement generally consists of three parts (separated by ";"):
计数for循环语句一般由三部分组成(用“;”分隔):
- A variable declaration that is executed before the loop starts, usually a counting variable (in your case "int x = 0")
- A break condition, if it's true we exit the loop, this is where your loop is wrong (Also your code says "x > 5" which means "x is greater than 5", contradicting your output saying "less than 5").
- Something that happens after each iteration, usually adding 1 to the count variable defined in the first step (you're missing this in your current code)
- 在循环开始之前执行的变量声明,通常是一个计数变量(在您的情况下为“int x = 0”)
- 中断条件,如果为真,我们退出循环,这就是您的循环错误的地方(您的代码也说“x > 5”,这意味着“x 大于 5”,与您的输出说“小于 5”相矛盾)。
- 每次迭代后发生的事情,通常将 1 添加到第一步中定义的计数变量(您在当前代码中缺少此)
When iterating over an array and trying to find elements that match a specific criteria (in this case "number less than 5") you should not do that in the loop condition (step 2 above), if this is true for any one number in your array the loop will stop executing. You should probably check that with an if-statement inside your loop.
当迭代一个数组并试图找到匹配特定条件的元素(在这种情况下“数字小于 5”)时,您不应该在循环条件(上面的第 2 步)中这样做,如果这对于任何一个数字都是真的您的数组循环将停止执行。您可能应该在循环中使用if语句进行检查。
for (int x = 0; x < numbers.length; x++) {
if (/* number is less than 5 */) {
// print number
}
}
Now we have a for-loop that will go over the whole array by counting x from 0 (1st part of the for-statement, "int x = 0") until we reach the end of the array (2nd part, "x < numbers.length") in steps of 1 (3rd part, "x++". Could also be written as "x = x + 1"). In each iteration we can now check the element of the array (numbers[x]) for being less than 5 with an if-statement, and if it is print it.
现在我们有一个 for 循环,它将遍历整个数组,从 0(for 语句的第一部分,“int x = 0”)到我们到达数组的末尾(第二部分,“x < numbers.length") 以 1 为步长(第 3 部分,“x++”。也可以写成“x = x + 1”)。在每次迭代中,我们现在可以使用if语句检查数组 (numbers[x]) 的元素是否小于 5 ,如果是则打印它。
回答by Mosa
Calculate the average:
计算平均值:
int average = sum / numbers.length;
All numbers higher than the average:
所有高于平均值的数字:
for(int x = 0; x < numbers.length; x++) {
if(numbers[x] > average){
System.out.println(numbers[x]);
}
}
Number less than 5:
小于 5 的数字:
for(int x = 0; x < numbers.length; x++) {
if(numbers[x] < 5){
System.out.println(numbers[x]);
}
}
Please consider how you set your braces in the loop. Its like this:
请考虑如何在循环中设置大括号。就像这样:
for (int i = 0; i < x; i++){
// do stuff
}
回答by Jonatan
In java 8 you can use lambda expressions to achieve the same result as in a for loop but shorter. Read more on lambda expressions here: Lambda Expressions
在 Java 8 中,您可以使用 lambda 表达式来实现与 for 循环相同但更短的结果。在此处阅读有关 lambda 表达式的更多信息:Lambda 表达式
Integer[] array = {1,2,3,4,5,6};
int sum = Arrays.stream(array).reduce(0, (a, b) -> a + b);
List<Integer> result = Arrays.stream(array)
.filter(x -> sum*1.0/array.length <= x && x < 5 )
.collect(Collectors.toList());