简体   繁体   English

我的switch语句没有返回任何内容?

[英]My switch statement is not returning anything?

Im selecting a "Left" from alert dialog and after putting it into convertStatusToCode function should retrun "4" but its not? 我从警告对话框中选择一个“左”,并将其放入convertStatusToCode函数后应该返回“4”,但不是吗?

final String[] status = {"Left"};

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Pick a Status");
builder.setItems(status, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        BookingStatus = convertStatusToCode(status[which]);

HERE IS MY convertStatusToCode FUNCTION but it is not retruning "4" which it should return. 这是我的convertStatusToCode功能,但它不会返回它应该返回的“4”。


private String convertStatusToCode(String status) {

    switch (status) {
        case "Processing":
            i = "0";
            break;
        case "Room is Allotted":
            i = "1";
            break;
        case "Sorry All Rooms Are Full":
            i = "2";
            break;
        case "Living":
            i = "3";
            break;
        case "Left":
            i = "4";
            break;
    }
    return i;
}

it should look something like this 它应该看起来像这样

  String getStatusCode(String status) {
        switch (status) {
            case "Processing":
                return "0";
            case "Room is Allotted":
                return "1";
            case "Sorry All Rooms Are Full":
                return "2";
            case "Living":
                return "3";
            case "Left":
                return "4";
            default:
                return "default_value";
        }
    }

Based upon what you've added in the comments, your Switch statement (albeit incomplete) shouldn't be the cause of your problem as the other posters are assuming. 根据您在评论中添加的内容,您的Switch语句(尽管不完整)不应该是您的问题的原因,正如其他海报所假设的那样。

You may have a problem with the completness of your switch cases, but that shouldn't stop the switch to work as intended. 您的switch案例的完整性可能有问题,但这不应该阻止交换机按预期工作。

You're saying that you DEBUG this and that i has the value of "" which I cannot understand unless you are not having a return value in scope. 你说你调试这个并且i""的值,除非你没有范围内的返回值,否则我无法理解。

The answer posted by Antonis Radz is how I would write that switch if I had to. Antonis Radz发布的答案就是如果我不得不写那个开关的话。

If you write your Convert function to return like so, then you don't need the extra i variable, which we don't know where you declared. 如果你将Convert函数写成这样返回,那么你不需要额外的i变量,我们不知道你声明的位置。

If you debug this line of code (like you said you did) 如果你调试这行代码(就像你说的那样)

switch(value) and you're telling us that value == "Left" then the switch is receiving the correct value at runtime. switch(value) ,你告诉我们value == "Left"然后开关在运行时接收到正确的值。

Now your "Case" is executed (when you set a breakpoint in case "Left" . So the switch is, again, performing its function so far. 现在你的“案例”被执行(当你设置一个断点case "Left" 。所以开关再次执行它的功能到目前为止。

Then I assume you added another breakpoint in the following line(s): 然后我假设您在以下行中添加了另一个断点:

            i = "4";
            break;

to see what is going on. 看看发生了什么。

The first breakpoint would hit in the assignment i= ... and so the variable i which I assume is declared somewhere in scope as String i (At the very least), is either null or contains an old value. 第一个断点将在赋值i= ...中命中,因此我假设的变量i在范围内被声明为String i (至少),是null或包含旧值。 Doesn't matter because the next breakpoint (in the break line) would be reached after i is assigned the value of the String "4" . 无关紧要,因为在i被赋予字符串"4"的值后,将到达下一个断点(在break行中)。 So if you add a watch to i and inspect it right there, it must have the value of "4". 因此,如果您向i添加一个手表并在那里检查它,它必须具有值“4”。

Then you return this i so, again, it should still be "4". 然后你回到这个i的话,再次,它应该仍然是“4”。

If you did all this, you would have been able to tell much closer where the variable is not being assigned. 如果你做了这一切,你就能够更加接近地分配变量的位置。

Do like this. 这样做。

    public String getstatuscode(String status){
        String i="";
        switch (status) {
            case "Processing":
                i = "0";
                break;
            case "Room is Allotted":
                i = "1";
                break;
            case "Sorry All Rooms Are Full":
                i = "2";
                break;
            case "Living":
                i = "3";
                break;
            case "Left":
                i = "4";
                break;
            default:
                i = "-1";

        }
        return i;
    }

Declare i as String or int as your requirement. 声明我作为String或int作为您的要求。 Method is same.Don't forget to remove quotes if you want to assign i as int. 方法是相同的。如果要将i指定为int,请不要忘记删除引号。

Another thought to use enum rather than switch-case here which gives more flexibility & reusability. 另一个想法是使用enum而不是switch-case,这提供了更多的灵活性和可重用性。

public enum BookingStatus
{

    PROCESSING("Processing", 1),
    ALLOTED_ROOM("Room is Allotted", 2),
    ALL_FULL("Sorry All Rooms Are Full", 3),
    LIVING("Living", 4),
    LEFT("LEFT", 5);

    private String status;
    private int code;

    BookingStatus(String status, int code)
    {
        this.status = status;
        this.code = code;
    }

    public static BookingStatus getCodeFromStatus(String status)
    {
        for (BookingStatus e : values()) {
            if (e.status.equals(status)) {
                return e;
            }
        }
        return null;
    }

    public static void main(String args[]) {
        System.out.printf("Code for this status is %s", BookingStatus.getCodeFromStatus("Sorry All Rooms Are Full").code);
    }

}
switch (status) {
        case "Processing":
            i = "0";
            break;
        case "Room is Allotted":
            i = "1";
            break;
        case "Sorry All Rooms Are Full":
            i = "2";
            break;
        case "Living":
            i = "3";
            break;
        case "Left":
            i = "4";
          // return "4";
            break;
    }
    return i;
}

use return "4"; 使用return "4"; you don't print anything in switch cases so switch can't return anything if you want to print in switch cases output then use the print method or return in any programming language. 如果你想在开关盒输出中打印然后使用print方法或以任何编程语言返回,你不会在开关盒中打印任何东西,所以开关不能返回任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM