【问题标题】:Using try-catch with a if else loop使用带有 if else 循环的 try-catch
【发布时间】:2016-10-26 08:34:14
【问题描述】:

所以我正在制作一个程序,它从命令行获取一个 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.

我怎样才能使它在找到数字时只打印索引行,反之亦然。我觉得这可能与循环有关,但不确定我能做些什么来防止这种情况发生。

【问题讨论】:

  • 将循环放在 try-block 内,而不是将 try-block 放在循环内。或者更好的是,为什么还要有一个try-block?只需进行搜索,如果未找到,则打印错误消息。
  • 把你的 try-catch 放在 for 循环外面,而不是里面。
  • 您每次都抛出异常以检查数组中的元素的值并发现不匹配,这是不正确的,因为只有在访问所有元素后才能断定该元素不在数组中。
  • 如果您打算将异常从函数中抛出,为什么要捕获异常?
  • @AvenNova 阅读了我的回答:您根本不应该尝试捕获。你应该抛出一个异常,而不是捕获它。

标签: java arrays methods command-line try-catch


【解决方案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】:

    你可以像下面这样重写代码..

    根据您的代码,您正在检查每个元素,如果不匹配,则抛出异常。这是不准确的,因为除非您扫描所有元素,否则您无法得出该元素是否不存在的结论。在这种情况下,您可以使用标志来确定是否找到匹配项,并且在 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.");
    }
    

    【讨论】:

    • 虽然这可行,但我必须根据要求抛出异常。谢谢你的帮助...
    • 我已经更新了答案..从我之前的代码中,你只需要将return -1替换为抛出异常的语句..
    • 这可行,但仍会引发红色异常文本。因此,当我在 main 中调用 FindNum 方法以仅打印文本时,我使用了 try catch,它现在可以工作了!感谢您的帮助!
    【解决方案3】:

    在程序中的数组中搜索 int,如果找到,则返回数字的索引。如果没有,则抛出异常,说明未找到并结束程序。

    仔细阅读你写的内容。

    如果没有找到,你应该抛出一个异常。你只有在经历了它的所有元素后才知道你还没有找到价值。所以你不能从循环内部抛出。如果你还没有找到元素,你只能在循环之后抛出。

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

    最后,您的方法应该返回索引。但事实并非如此。它总是返回 -1。

    【讨论】:

    • 谢谢!您和 redflar3 的建议帮助了很多人,终于明白了!我希望我可以选择两个答案而不是一个:D
    【解决方案4】:

    删除 try 中的 else 和 throw 语句。初始化一个变量 boolean flag=false。迭代前。如果在循环中找到元素,则添加条件 flag=true 然后从循环中中断。现在在迭代结束后写下: if(!flag) System.out.print("Element not found");基本上,您打印的是在每次迭代中都找不到的元素,除非找到了元素,这是一个可怕的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多