【问题标题】:Array needle in haystack大海捞针阵列针
【发布时间】:2018-11-21 22:44:42
【问题描述】:

我有一个任务来创建一个 int 数组,该数组在另一种方法中为用户输入 int 值进行搜索,然后显示该元素在数组中的索引。我的那部分工作得很好,我个人选择使数组中的元素随机值从 1 到 10。我还需要让程序显示一条消息(“在数组中找不到元素”),以防给定number 不在数组中。我似乎无法让这部分正常工作,希望我能在这里得到一些建议。

import java.util.Scanner;

public class NeedleInHaystack {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Please provide an Integer for the needle: ");
    int needle = scan.nextInt();
    int[] haystack = new int[10];

    System.out.println("The array being used is: ");

    for (int i = 0; i < 10; i++) {
        int j = (int) (Math.random() * 9 + 1);
        haystack[i] = j;
        System.out.print(haystack[i] + " ");
    }

    returnIndex(haystack, needle);

}

public static int returnIndex(int[] haystack, int needle) {
    int index = needle;
    System.out.println("\nThe needle is found at index: ");

    for (int i = 0; i < haystack.length; i++) {
        if (haystack[i] == index) {
            System.out.println(i);
        }
    }

    return index;
 }

}

该程序是一个 int needle 在一个 hastack 数组中。如果输入值不存在于随机数组中,让程序“优雅”结束的最佳方法是什么?

作业的措辞如下: “使用在整数数组中搜索指定整数值的方法创建一个 Java 程序(请参阅下面有关启动方法标题的帮助)。如果数组包含指定的整数,则该方法应返回其在数组中的索引。如果不是,该方法应该抛出一个异常,说明“在数组中找不到元素”并正常结束。使用您创建的数组和用户输入的“针”测试 main 中的方法。”

【问题讨论】:

  • 我们对您想问的一个问题感兴趣,但我不知道您的问题是什么。我们对阅读您的整个计划或评论您的一些目标不太感兴趣。确实,您需要将其范围缩小到您所要求的范围内。
  • 简单的问题是如何为这个程序添加一个意外事件 - 如果输入 int 不在数组中打印“数组中找不到元素”

标签: java arrays


【解决方案1】:

问题出在for-loop 你正在检查数组中的needle 但没有返回它,而且由于你在开始时分配了int index = needle;,即使针不在数组中它也会返回针

所以在这种情况下,在开头分配 index=0 并迭代数组,如果找到则返回 index 否则返回 needle

public static int returnIndex(int[] haystack, int needle) {
int index;
System.out.println("\nThe needle is found at index: ");

for (index = 0; index < haystack.length; index++) {
    if (haystack[index] == needle) {
      System.out.println("value found at index"+index);
        return index;
    }
   }
   System.out.println("The value not found in array");

   return needle;;
}

【讨论】:

    【解决方案2】:

    您可以在方法中添加某种boolean 标志,如果找到它,则设置为true。如果不是,该标志将保持为假。遍历数组后,您可以检查标志是否为假,如果是,则可以打印一些错误消息。

    public static int returnIndex(int[] haystack, int needle) {
    
        boolean found = false;
    
        int index = needle;
        System.out.println("\nThe needle is found at index: ");
    
        for (int i = 0; i < haystack.length; i++) {
            if (haystack[i] == index) {
                System.out.println(i);
                found = true;
            }
        }
    
        if (found) return index;
    
        else  {
    
            System.out.println("Not found.");
            return null;
    
        }
    
    
     }
    
    }
    

    【讨论】:

      【解决方案3】:

      由于您应该返回索引而不是针值,因此一旦找到对象,您就应该在 for 循环内返回。与 Deadpool 所做的非常相似。

      但是正如您所说,当找不到针时,该方法应该抛出异常,您应该摆脱第二个 return 语句并抛出异常。

      public static int returnIndex(int[] haystack, int needle) {
          System.out.println("\nThe needle is found at index: ");
      
          for (int i = 0; i < haystack.length; i++) {
              if (haystack[i] == needle) {
                  System.out.println(i);
                  return i;
              }
          }
      
          throw new NoSuchElementException(„Element not found in array“);
       }
      

      【讨论】:

        猜你喜欢
        • 2011-02-07
        • 2016-07-30
        • 1970-01-01
        • 2010-11-17
        • 2018-04-18
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多