有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用带有if-else循环的trycatch

因此,我正在制作一个程序,从命令行获取int输入,然后在程序中搜索数组中的int,如果找到,返回数字的索引。如果没有,则抛出一个异常,说明找不到它并结束程序。这就是我到目前为止所做的:

public static void main(String[] args) {

    int[] intArray = {9, 97, 5, 77, 79, 13, 7, 59, 8, 6, 100, 55, 35, 89, 74, 66, 32, 47, 51, 88, 23};
    System.out.println(Arrays.toString(intArray));

    int intArgs = Integer.parseInt(args[0]);

    System.out.println("Your entered: " + intArgs);

    FindNum(intArray, intArgs);
}

public static int FindNum(int[] intArray, int intArgs) {
    for (int index = 0; index < intArray.length; index++){
        try{
            if (intArray[index] == (intArgs))
                System.out.println("Found It! = " + index);
            else 
                throw new NoSuchElementException("Element not found in array.");
        } catch (NoSuchElementException ex){
            System.out.println(ex.getMessage());
        }
    }
    return -1;      
}

虽然这种方法可以工作并且可以找到索引,但它会对数组中的每个数字抛出异常,而不是对整个数组抛出一个异常。如果它在数组中找到了数字,那么它会用循环中的确认行替换其中一行。66的输出示例:

[9, 97, 5, 77, 79, 13, 7, 59, 8, 6, 100, 55, 35, 89, 74, 66, 32, 47, 51, 88, 23]
Your entered: 66
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Found It! = 15
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.
Element not found in array.

如何使它在找到编号时只打印索引行,而对于异常情况,则反之亦然。我觉得这可能与循环有关,但不确定我能做些什么来防止这种情况


共 (4) 个答案

  1. # 1 楼答案

    int i = -1;
    ...
        if (intArray[index] == intArgs) i = index;
    ...
    

    尝试创建一个int变量来保存找到的元素的索引。如果未找到任何内容,该方法的最后一行将引发异常

    if (index == -1) throw new NoSuchElementException(...);
    

    另一种方法是直接返回索引,最后在没有任何条件的情况下抛出异常

        if (intArray[index] == intArgs) return index;
    ...
    throw new NoSuchElementException(...);
    

    我建议您不要返回错误代码。在本例中,对Java来说更自然的是抛出一个未经检查的异常

  2. # 2 楼答案

    删除try中的else和throw语句。初始化变量布尔标志=false。在迭代之前。如果在循环中找到元素,则添加条件标志=true,然后从循环中断。现在,在迭代结束后,编写以下代码:if(!flag)系统。出来打印(“未找到元素”);基本上,您正在打印在每次迭代中都找不到的元素,除非元素被找到,这是一个可怕的错误

  3. # 3 楼答案

    searches the array in the program for the int and if found, return the index of the number. If not, then it throws an exception stating that it wasn't found and end the program.

    仔细阅读你写的东西

    如果未找到,则应抛出异常。你只知道,一旦你通过了它的所有元素,你还没有找到它的价值。所以你不能从圈内扔。如果未找到元素,则只能在循环后抛出

    其次,您应该抛出异常。您没有这样做:而是抛出并捕获刚才抛出的异常。抛出异常的目的是让方法的调用方知道发生了异常。您不应该在方法中抛出并捕获异常

    最后,您的方法应该返回索引。但它没有。它总是返回-1

  4. # 4 楼答案

    您可以像下面这样重写代码

    根据您的代码,您正在检查每个元素,如果不匹配,您将抛出一个异常。这并不准确,因为除非扫描所有元素,否则无法断定元素是否不存在。在这种情况下,您可以使用标志来确定是否找到匹配项,在for循环之后,您可以检查标志并引发异常。同样,如果您计划在同一个方法中捕获异常,那么您可能根本不需要抛出异常。相反,您可以简单地返回索引或-1,如下所示

    public static int FindNum(int[] intArray, int intArgs) {
       for (int index = 0; index < intArray.length; index++){
            if (intArray[index] == (intArgs)){
                System.out.println("Found It! = " + index);
                return index;
            }
       }
       throw new NoSuchElementException("Element not found in array.");
    }