Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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/8/svg/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,我的代码正在打印“数组中找不到元素”,打印次数与在数组中找到该数字所需的次数相同。我为什么以及如何修复该数字 我的任务: 使用一个方法创建一个Java程序,该方法在整数数组中搜索指定的整数值(请参阅下面启动方法标题的帮助)。如果数组包含指定的整数,则该方法应返回其在数组中的索引。如果不是,该方法应该抛出一个异常,声明“数组中找不到元素”,并优雅地结束。主要使用您制作的数组和用户输入的“针”来测试该方法 公共静态int返回索引(int[]干草堆,int针){ 到目前为止,我的代码是: import

我的代码正在打印“数组中找不到元素”,打印次数与在数组中找到该数字所需的次数相同。我为什么以及如何修复该数字

我的任务:

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

公共静态int返回索引(int[]干草堆,int针){

到目前为止,我的代码是:

import java.util.Scanner;
public class Assignment1 {

 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();
     returnIndex(haystack,needle);

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


在不中断循环的情况下,您多次获得相同的输出。此外,如果元素不在数组中,则仅当您遍历整个数组时,才将
System.out.println(“数组中未找到元素”);
移到for循环之外。下面是完整的示例,请查看

在下面的代码中,如果数组中找不到元素,我将中断循环

public class SOTest {
    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();
        int index = returnIndex(haystack, needle);
        if(index!=-1) // print index only if element is in array.
        System.out.println("Element found at index : " + index);

    }

    public static int returnIndex(int[] haystack, int needle) {
        for (int n = 0; n < haystack.length; n++) {
            if (haystack[n] == needle)
                return n;
        }
        System.out.println("Element not found in array");
        return -1;

    }
}
对于数组中的数字5,输出将为低:-

Enter a number in the array: 
5
Element found at index : 1

在不中断循环的情况下,您多次获得相同的输出。此外,如果元素不在数组中,则仅当您遍历整个数组时,才将
System.out.println(“数组中未找到元素”);
移到for循环之外。下面是完整的示例,请查看

在下面的代码中,如果数组中找不到元素,我将中断循环

public class SOTest {
    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();
        int index = returnIndex(haystack, needle);
        if(index!=-1) // print index only if element is in array.
        System.out.println("Element found at index : " + index);

    }

    public static int returnIndex(int[] haystack, int needle) {
        for (int n = 0; n < haystack.length; n++) {
            if (haystack[n] == needle)
                return n;
        }
        System.out.println("Element not found in array");
        return -1;

    }
}
对于数组中的数字5,输出将为低:-

Enter a number in the array: 
5
Element found at index : 1

您需要将打印线置于循环之外:

public static int returnIndex(int[] haystack, int needle) {
        for (int n = 0; n < haystack.length; n++) {
            if (haystack[n] == needle) 
                return n;
        }

        System.out.println("Element not found in array");
        return -1;  
 }

您需要将打印线置于循环之外:

public static int returnIndex(int[] haystack, int needle) {
        for (int n = 0; n < haystack.length; n++) {
            if (haystack[n] == needle) 
                return n;
        }

        System.out.println("Element not found in array");
        return -1;  
 }

尝试将System.out置于return-1上方,这样您就不会每次找不到所选数字时都打印“数组中找不到元素”。

尝试将System.out置于return-1上方,这样您就不会每次找不到所选数字时都打印“数组中找不到元素”。

尝试此操作

只需对returnIndex函数做一些小的更改,它就会对您起作用

publicstaticintreturnindex(int[]haystack,int-needle){
对于(int n=0;n
试试这个

只需对returnIndex函数做一些小的更改,它就会对您起作用

publicstaticintreturnindex(int[]haystack,int-needle){
对于(int n=0;n
提示:
System.out.println(“数组中找不到元素”);
不属于循环内部。请在
else
块中删除
System.out
,并在
returnIndex
方法返回后检查结果。根据问题描述处理结果。在
中(int n=0;n
每次出现条件
(haystack[n]==指针)
时,您都会打印数组中找不到的元素。请尝试用它替换
return-1;
。提示:
System.out.println(“数组中找不到元素”)
不属于循环内部。删除
系统。在
else
块中取出
,并在
returnIndex
方法返回后检查结果。根据问题描述处理结果。在
for中(int n=0;n
每次出现条件
(haystack[n]==pinder)
时,您都会打印数组中找不到的元素。请尝试用它替换
返回-1;
。尝试输入5。@KevinAnderson,在添加修改代码时发现了它。谢谢您的反馈,但出于某种原因,当我输入一个不在数组中的数字时,“…不在数组中”以及“…在索引:-1处找到”如何修复它?@michael,只需添加一个条件以打印它,如果它不是-1。感谢您的帮助@AmitKTry输入5。@KevinAnderson,在添加修改代码时找到它谢谢您的反馈,但出于某种原因,当我输入的数字不在它打印的数组中时“…不在数组中”以及“…在索引:-1处找到”我如何修复它?@michael,只需添加一个条件以打印它,如果它不是-1。谢谢您的帮助@AmitK
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 void main(String[] args) {
     ...              
     int needle = sc.nextInt();
     if (returnIndex(haystack,needle) == -1){
           System.out.println("Element not found in array");
     }
     else {
           System.out.println("Element found in array");
     }
}
public static int returnIndex(int[] haystack, int needle) {
    for (int n = 0; n < haystack.length; n++) {
        if (haystack[n] == needle) {
            return n;
        } else {
            if(n == haystack.length - 1)
            System.out.println("Element not found in array");
        }
    }
    return -1;
}