【问题标题】:Array Value Output more than one数组值输出多个
【发布时间】:2017-11-12 05:00:10
【问题描述】:

这是我想出的代码。但是,我希望能够输出:

  • (值)在插槽 x 中。
  • (值)在插槽 x 中。

如果 (value) 在两个插槽中可用,则输出两个 - 如 7。

  • (num) 不在数组中。

但不是两者兼而有之。 有人可以帮忙吗?

    public static void main(String[] args) {
    int search, counter;
    int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};

    System.out.print("Array: ");
    for (int count=0; count<num.length; count++)
        System.out.print(+num[count] + " ");

    Scanner in = new Scanner (System.in);
    System.out.print("\nValue to find: ");
    search = in.nextInt();

    for (counter = 0; counter < num.length; counter++ ){
        if (num[counter] == search)
        {
            System.out.println(search + " is in slot " + (counter + 1) + ".");
        }           
    }
    if (counter == num.length)
        {
            System.out.println(search + " is not in the array.");
        }
}
}

【问题讨论】:

    标签: java arrays loops conditional-statements linear-search


    【解决方案1】:

    虽然我觉得您可能应该在另一个社区(例如 https://codereview.stackexchange.com/)上提出这样的问题,但我可以提供一个建议:

    使用布尔标志来检查您之前是否找到过它。像这样的:

    public static void main(String[] args) {
      int search;
      boolean found = false;
      int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};
    
      System.out.print("Array: ");
      for (int count=0; count<num.length; count++)
          System.out.print(+num[count] + " ");
    
      Scanner in = new Scanner (System.in);
      System.out.print("\nValue to find: ");
      search = in.nextInt();
    
      for (int counter = 0; counter < num.length; counter++ ) {
          if (num[counter] == search)
          {
            System.out.println(search + " is in slot " + (counter + 1) + ".");
            found = true;
          }           
      }
    
      if (!found) {
            System.out.println(search + " is not in the array.");
      }
    
      in.close();
    
    }
    

    因此,只有在线性遍历数组后找不到元素时才会打印“未找到”消息...

    【讨论】:

    • 假设使用布尔标志是最合适的解决方法。
    【解决方案2】:

    您的代码的问题是您检查计数器是否已达到数组长度。总会发生的事情。您应该检查是否找到了值。

    这应该可以解决问题:

    public static void main(String[] args) {
        int search, counter;
        int num[]={3, 4, 5, 6, 7, 8, 10, 7,  9, 13};
        boolean wasFound = false;
    
        System.out.print("Array: ");
        for (int count=0; count<num.length; count++)
            System.out.print(+num[count] + " ");
    
        Scanner in = new Scanner (System.in);
        System.out.print("\nValue to find: ");
        search = in.nextInt();
    
        for (counter = 0; counter < num.length; counter++ ){
            if (num[counter] == search)
            {
                System.out.println(search + " is in slot " + (counter + 1) + ".");
                wasFound = true;
            }
        }
        if (!wasFound)
        {
            System.out.println(search + " is not in the array.");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 2013-03-28
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多