无法访问的代码 - 在switch语句中返回后放置一个中断 [英] Unreachable Code - Placing a break after a return in switch statement

查看:99
本文介绍了无法访问的代码 - 在switch语句中返回后放置一个中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过这样做,虽然其他人已经问过这个问题,但他们的情况适用于不同的事情(据我所知)。

I've tried looking this up, and although other people have asked this, their situation applies to different things (as far as I can tell).

I我正在学习Java,我正在创建一个与用户交谈,提出问题和内容的程序。作为实际学习面向对象编程概念的一个步骤,我创建了一个类,它可以帮助我的主项目不会处理问题,而是将大多数问题的处理和返回放在一个名为ConversationHelper的类中。

I am learning Java, and I am creating a program that "talks" with the user, asking questions and stuff. As a step to actually learning the concepts of object-oriented programming, I created a class that helps my main project NOT be filled with the handling of questions, instead I put the handling and returns for most questions in a class called ConversationHelper.

我在ConversationHelper中创建了一个方法,用于询问是/否问题。这是它的代码:

I created a method in my ConversationHelper for asking Yes/No questions. Here is its code:

public boolean askYNQuestion(String question) {
    Scanner input = new Scanner(System.in);

    boolean isInputCorrect = false;

    while(isInputCorrect == false) {
    displayQuestion(question + " (Y or N)");
    String readin = input.nextLine();

    switch (readin.toLowerCase()) {
        case "n":
            return false;
            break;
        case "y":
            return true;
            break;
        case "yes":
            return true;
            break;
        case "no":
            return false;
            break;
        case "false":
            return false;
            break;
        case "true":
            return true;
            break;
        default:
            break;
    }
}
    System.out.println("THIS IS NOT SUPPOSED TO HAPPEN! FALSE DEFAULT!");
    return false;
}

这个问题比其他任何问题更令人烦恼。 switch语句中的每个中断都会出现无法访问的代码,因为它之前有一个return语句。但是,这是打算发生的。

The problem with this is more of an annoyance than anything else. Every break in the switch statement comes up with "unreachable code" because there is a return statement before it. However, this is meant to happen.

Netbeans现在告诉我,在构建时,编译器运行时出错。这不仅令人讨厌,而且很难判断错误是否是这个已知错误,或者是否是需要我注意的另一个错误,在我尝试使我的程序工作时浪费时间。

Netbeans now tells me, at build, that the "compiler ran with errors." Not only is this just annoying, but it makes it hard to tell if the error is this known error, or if it is another error that needs my attention, wasting time when I'm trying to make my program work.

我正在寻找一种不会产生错误的更好方法,或者强制禁用此错误的方法。我正在使用Java 8运行Netbeans。非常感谢任何帮助。

I'm looking for either a better way to do this that won't generate errors, or a way to force disable this error from coming up. I am running Netbeans, with Java 8. Any help would be greatly appreciated.

谢谢!

推荐答案

返回将作为中断。没有必要同时使用两者。如果您收到的消息是您有无法访问的代码,那么它就没有任何意义,或者您以前做过的事情在逻辑上是无序的。

A return will act as a break. There is no need to use both. If you receive the message that you have unreachable code, then there is either no point in it being there, or something you did previously is out of order logically.

作为一个第二点,只是为了改进你的代码,你可以在switch语句中组合条件。如果你有多件商品会返回同样的东西,你可以一个接一个地列出它们,然后为它们全部单独退货。

As a second point, just to improve your code, you can combine conditions in your switch statement. If you have multiple items which will return the same thing, you can list them one after the other and put a single return for them all.

 switch (readin.toLowerCase()) {
    case "n":
    case "no":
    case "false":
        return false;
    case "y":
    case "yes":
    case "true":
        return true;
    default:
        break;
}

这篇关于无法访问的代码 - 在switch语句中返回后放置一个中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆