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

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

问题描述

我试过查这个,虽然其他人问过这个,但他们的情况适用于不同的事情(据我所知).

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

我在我的 ConversationHelper 中创建了一个方法来询问是/否问题.这是它的代码:

public boolean askYNQuestion(字符串问题){扫描仪输入 = 新扫描仪(System.in);boolean isInputCorrect = false;while(isInputCorrect == false) {displayQuestion(question + " (Y or N)");String readin = input.nextLine();开关 (readin.toLowerCase()) {案例n":返回假;休息;案例y":返回真;休息;情况是":返回真;休息;情况否":返回假;休息;案例假":返回假;休息;案例真":返回真;休息;默认:休息;}}System.out.println("这不应该发生!错误默认!");返回假;}

这个问题比其他任何事情都更令人烦恼.switch 语句中的每个中断都会产生无法访问的代码",因为在它之前有一个 return 语句.然而,这是注定要发生的.

Netbeans 现在在构建时告诉我编译器运行时出错".这不仅很烦人,而且很难判断错误是这个已知错误,还是另一个需要我注意的错误,在我试图让我的程序工作时浪费时间.

我正在寻找一种不会产生错误的更好的方法,或者一种强制禁止出现此错误的方法.我正在使用 Java 8 运行 Netbeans.任何帮助将不胜感激.

谢谢!

解决方案

return 将充当 break.没有必要同时使用两者.如果您收到消息说您有无法访问的代码,那么它在那里没有任何意义,或者您之前所做的事情在逻辑上是无序的.

作为第二点,为了改进您的代码,您可以在 switch 语句中组合条件.如果您有多个返回相同内容的项目,您可以将它们一个接一个地列出,并为它们全部输入一个返回值.

 开关 (readin.toLowerCase()) {案例n":情况否":案例假":返回假;案例y":情况是":案例真":返回真;默认:休息;}

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 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.

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;
}

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 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.

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.

Thanks!

解决方案

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.

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天全站免登陆