Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在干草堆(数组)中查找针(用户输入)_Java - Fatal编程技术网

Java 在干草堆(数组)中查找针(用户输入)

Java 在干草堆(数组)中查找针(用户输入),java,Java,我正在尝试创建一个程序来确定是否在数组中找到用户输入 然后输出整数在数组中的位置 看起来我的程序没有从我的主程序中提取任何内容 节目如下: public static int returnIndex(int[ ] haystack, int needle) { for (int n : haystack) { if (haystack[needle] == n ) { } else { Syst

我正在尝试创建一个程序来确定是否在数组中找到用户输入

然后输出整数在数组中的位置

看起来我的程序没有从我的主程序中提取任何内容

节目如下:

public static int returnIndex(int[ ] haystack, int needle) {    
    for (int n : haystack) {       
        if (haystack[needle] == n )  {       
        } else {
            System.out.println("Element not found in array");                   
            System.exit(0);             
       }
      }
      return needle;        
    }

 public static void main(String[] args) {
     int[] haystack = { 4,5,6,7,12,13,15,16,22,66,99,643 };        
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter a number in the array: ");                
     int needle = sc.nextInt(); 
   }
 }

你走错了路

使用循环计数

for (int index=0; index haystack.length; index++) {
...
相反!然后将
haystack[index]
needle
进行比较,并在匹配中返回
index


请注意:实际上,像您在问题中所做的那样使用“for each”循环样式是一种很好的做法——这应该是写下循环以迭代数组/集合项时的“第一”想法。但有时你只需要知道
索引
;然后计数是正确的选择。

您应该循环数组的索引,而不是值。一旦找到一个返回您希望使用的值的索引,您就可以拉取它。但是,如果对数组进行排序,则可以使用算法简化。我能想到的最简单的方法是,在数组中间检查值是否较大、较小或相同,然后在该值和下一个较大或较小的值之间分别减半,直到找到答案。

要这样解决问题,您需要跟踪索引,并将每个值与
指针进行比较,如下所示(如果不在数组中,则返回
-1
):

如果您想使用索引来实现这一点,那么它将如下所示:

public static int returnIndex(int[] haystack, int needle) {

    for (int n = 0; n < haystack.length; n++) {

        if (haystack[n] == needle) {
            return n;
        }

    }

    return -1;
}

我认为
needle
是用户输入的号码,您可以在
数组中找到它,然后打印位置。如果我走对了路,那就改变

if(干草堆[n]==n)
to
if(干草堆[n]==n)


返回数字位置的n
。并使用前面描述的@GhostCat计数循环。其中
n
索引

您的逻辑稍微向后一点,而不是返回第一个不匹配值,您应该返回第一个匹配值,但是,您应该使用
for循环的长形式
,这样您就知道索引:p对于记录:我在我的答案中输入了更多信息。。。我认为:你不会看到太多其他的输入;因此,考虑接受/投票(如你将达到投票级别;-)你发现最有用的答案。所以我会做一些类似于<代码>的事情。(int index=0;indexahh..我现在明白我是怎么想的了。感谢您花时间解释我的代码中的错误;程序的目的是输出整数索引或显示消息,然后退出程序。由于您将消息作为
else
语句直接放在for循环中,因此它不会如果由于
系统原因针不在第一个索引中,请不要检查阵列的其他元素。退出(0)
public static int returnIndex(int[] haystack, int needle) {

    for (int n = 0; n < haystack.length; n++) {

        if (haystack[n] == needle) {
            return n;
        }

    }

    return -1;
}
public static int returnIndex(int[] haystack, int needle) {

    // iterates each value of haystack
    for (int n : haystack) {
        // this checks for a value at index needle, if needle were 300 it
        // looks for haystack[300]
        // if haystack were a 10 index array it would lead to an error
        if (haystack[needle] == n) {

        }
        // using this else statement will lead to exiting program
        // the first time `haystack[needle] == n` is false this function
        // finishes
        else {
            System.out.println("Element not found in array");
            System.exit(0);
        }
    }

    return needle;
}