【问题标题】:Using a for loop to search array使用 for 循环搜索数组
【发布时间】:2015-06-11 14:49:52
【问题描述】:

我正在处理一项任务,但被困在一个部分。搜索我的数组以查看它是否包含数字 4。我也遇到了该答案重复 10 次的问题。我知道为什么会这样,只是不知道如何解决。从昨天开始我就一直在研究这个问题,直到我进入这个部分之前都很顺利。任何可以为我指明正确方向的建议都会很棒。下面是程序;

import java.util.*;


public class Array {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {


    int[] running = new int[10];

    int x;
    int sum = 0;
    int number;



    for(x = 0; x < 10; x++)
        {
            running[x] = (int) (x*2.6 + 2.6);
            System.out.println(running[x]);
        }

    for(x = 0; x < running.length; x++)
    {
        sum = sum + running[x];
    }

        System.out.println("The value for index 2 is: " + running[2]);
        System.out.println("The list length is: " + running.length);
        System.out.println("The total number of miles ran is: " + sum);
        System.out.println("The average number of miles ran is: " +(double)sum/running.length);

    for(x = 0; x < running.length; x++)
    {
        if (x == 4)
            System.out.println(4 + " Does not exist.");

        else
            System.out.println(4 + " does exist.");

    }
}

}

【问题讨论】:

  • 至少你的用户名是诚实的:)
  • 您的代码看起来不错。
  • 使用boolean 变量来存储,如果您找到了4 并在循环之后打印该结果。
  • 感谢大家的帮助。我正在慢慢地学习语言和编程规则。进入正确的思维框架对我来说很重要,当我陷入困境时,我会感到沮丧。很高兴有这个网站来检查答案和帮助。

标签: java arrays for-loop


【解决方案1】:

它打印了很多次,因为它在一个循环中。 此外,您的打印语句打印的信息有误。

for(x = 0; x < running.length; x++)
{
    if (running[x] == 4) {
        System.out.println(4 + " does exist.");
        return;
    }

}
   System.out.println(4 + " does not exist.");

【讨论】:

  • 你的代码将永远执行“不存在”,不管它是真是假。
  • @Error404 你错过了循环内的return 语句。
  • 但我真的不知道你为什么在那里使用回报。如果不是方法。我也在学习 Java,所以我对此并不安全,尽管它只用于方法。
  • 是的,你是对的......而且main() 也是一种方法......对吗???如果你看到 OP,这段代码就在 main 里面。
  • 是的,你说得对。但这到底是什么?完成程序?
【解决方案2】:

您正在检查您的循环索引,而不是实际的数组。

改变这个:

if (x == 4)

到这里:

if (running[x] != 4)

编辑:

如果您想要查看数组是否至少包含一次数字 4,然后打印出一次结果,那么您可以这样做:

boolean found = false;
for(x = 0; x < running.length; x++)
{
    if (running[x] == 4) {
        found = true;
        break;
    }
}

if (found) {
    System.out.println("4 does exist.");
} else {
    System.out.println("4 does not exist.");
}

【讨论】:

  • 谢谢,有帮助。虽然我不得不将 == 更改为 !=。此外,我仍然坚持打印 10 次的答案。我将如何将其更改为只显示一次?
  • @LostandConfused 如果您不想打印 10 次,则需要将 print 语句移到 for 循环之外。
【解决方案3】:

你应该像这样制作你的循环:

for(x = 0; x < running.length; x++)
{
    if (running[x] != 4)
       System.out.println(4 + " Does not exist.");
    else
       System.out.println(4 + " does exist.");
}

你必须引用数组的项目,而不是它的索引。

如果你只想得到一次答案,你必须这样做:

boolean correct = false;

    for(x = 0; x < running.length && correct == false; x++)
    {
      if (running[x] == 4)
          correct = true;
    }

    if(correct == true)
       System.out.println(4 + " does exist.");
    else
       System.out.println(4 + " Does not exist.");

您只需从循环中删除您的System.out.println

希望对你有所帮助!

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2021-06-27
    • 2016-05-21
    相关资源
    最近更新 更多