为什么此代码会产生"无法访问的语句"错误?

-3 java variables

该变量已在方法中声明,我想再次使用该变量,在if else语句的同一方法中.这是我声明的方法和变量.

我收到了"无法访问的声明"错误.

public void update() {

    System.out.println("'Back' to go back.");
    System.out.println("Enter Invoice Number:");
    String code = Input.getTextInput();
    if ("back".equalsIgnoreCase(code)) {
        return;
    }
    InvoiceDto invoiceToUpdate = invoiceService.readByCode(code);

    //Update the Invoice in memory
    System.out.println("Enter New Product ID:");
    String productId = Input.getTextInput();
    if ("back".equalsIgnoreCase(productId)) {
        return;
    } else {
        invoiceToUpdate.setProductId(productId);

        return;
    }

    invoiceToUpdate = invoiceService.readByCode(code);
    if (invoiceToUpdate == null) {
        System.out.println("Please enter a valid invoice code");

        return;
    } 
Run Code Online (Sandbox Code Playgroud)

Rea*_*tic 5

您收到"无法访问的代码"错误的原因是:

if ("back".equalsIgnoreCase(productId)) {
    return;
} else {
    invoiceToUpdate.setProductId(productId);

    return;
}
Run Code Online (Sandbox Code Playgroud)

此代码从"if"和"else"子句中的方法返回.这意味着在此之后流量永远不会到达生产线if.因此,您得到错误.

你可能需要重新思考你的逻辑.return将您发送回调用该方法的代码中的位置.你的方法的逻辑是没有意义的.