如何在数组中查找大于,小于或等于值的数字? [英] How to find numbers in an array that are greater than, less than, or equal to a value?

查看:484
本文介绍了如何在数组中查找大于,小于或等于值的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试输出小于5的值以及大于数组中所有值平均值的值。我不知道如何使这一切工作,并输出正确的数字。谁能帮忙?这是我的,我几乎在那里我只是不知道我在做什么错。

  {
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(数字倒序:);
{
for(int x = 0; x< numbers.length; ++ x)
System.out.println(numbers [x]);


System.out.println(所有八个数字的总和为:+ sum);
System.out.println(最低的数字是:+数字[0]);
System.out.println(最高的数字是:+数字[7]);
System.out.println(所有八个数字的平均值是:+ sum * 1.0 / 8);
System.out.println(所有高于平均值的数字是:);
{
for(int x = 0; x {numbers [x] = x + 1;}
int x = 0;
System.out.println(numbers [x]);
}
System.out.println(小于5的数字); (int x = 0; x {numbers [x] = x + 1;

{
; }
int x = 0;
System.out.println(numbers [x]);




$ b

解决方案



 

code> System.out.println(小于5的数字); (int x = 0; x< numbers.length& x> 5;){
numbers [x] = x + 1;





$ b

计数for循环语句通常由三部分组成(用 ):
$ b


  1. 在循环开始之前执行的一个变量声明,通常是一个计数变量(在你的情况int x = 0 )

  2. 中断条件,如果是,我们退出循环,这是你的循环出错的地方(你的代码也是x> 5 x大于5,与小于5的输出相矛盾)。
  3. 每次迭代后发生的事情,通常在第一步中定义的count变量加1你在当前代码中缺少这个元素)

当遍历一个数组并试图找到匹配特定条件的元素时这种情况下数字少于5),你不应该在循环条件(上面的第2步)中做到这一点,如果这是真的数组中的任何一个数字循环将停止执行。你可能应该在循环中用 if -statement来检查它。



pre $ for(int x = 0; x if(/ * number小于5 * /){
//打印号码
}
}

现在我们有一个for循环遍历整个数组,从0开始计算x(第一部分直到我们以1(第三部分,x ++)的步骤到达数组的末尾(第二部分,x< numbers.length),也可以写成 x = x + 1)。在每一次迭代中,我们现在可以用 if 语句检查数组的元素(数字[x]),如果打印它


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? 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]);
   }


}  

解决方案

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 ";"):

  1. A variable declaration that is executed before the loop starts, usually a counting variable (in your case "int x = 0")
  2. 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").
  3. 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)

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.

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.

这篇关于如何在数组中查找大于,小于或等于值的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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