簡體   English   中英

如果您的值不固定,如何使用 Enum?

[英]How to use Enum if your value is not fixed?

我知道我不清楚我的問題,但我在這里嘗試做的是使用枚舉來轉換溫度單位,而且我所知道的枚舉具有固定值,因此例如從華氏溫度轉換為攝氏溫度需要輸入計算才能工作但我不能使用主活動中的輸入,所以我試圖用它的 setter 聲明一個輸入,並使用類 Convert2 在主活動中設置輸入(類似於 c.setInput(input))並獲取它的值能夠在課堂上使用它,認為如果你得到值,你可以將它添加到常量中,然后使最后的返回值等於 1 乘以乘數,乘數是 = 到常量,但這不起作用,我被困在這里,因為我是枚舉的新手。 希望你能理解我在代碼中的意思。

public class Convert2 {

    private final double multiplier;

    private double input;

    public void setInput(double input) {
        this.input = input;
    }

    public double getInput() {
        return input;
    }

    public enum Unit2 {
        Fahrenheit,
        Celsius,
        Kelvin,
        ;

        public static Unit2 from(String text) {
            if (text != null) {
                for (Convert2.Unit2 unit2 : Convert2.Unit2.values()) {
                    if (text.equalsIgnoreCase(unit2.toString())) {
                        return unit2;

                    }
                }
            }

            throw new IllegalArgumentException("Cannot find a value for " + text);
        }
    }

    public Convert2(Convert2.Unit2 from, Convert2.Unit2 to) {
        double constant = 1;

        switch (from) {
            case Fahrenheit:
                if (to == Convert2.Unit2.Celsius) {
                    constant = (input - 32)
                            * 1.8;///here you can see how the calculation work as it needs the input value///


                } else if (to == Convert2.Unit2.Kelvin) {
                    constant = (5 / 9) + 273.15 - 32;
                }
                break;
        }
        multiplier = constant;

    }

    public double convert2(double input) {
        return (input - input + 1)
                * multiplier;///(input-input+1) gives 1 multiplied by the multiplier gives the constant value///
    }
}

您可以查看使用 switch 語句。 例如

如果你定義一個 Unit 枚舉如下

enum Unit {
    FAHRENHEIT,
    CELSIUS,
    KELVIN
}

一個完整的“轉換”方法將評估所有情況:

  1. f -> f

  2. f -> c

  3. f -> k

  4. c -> f

  5. c -> c

  6. c -> k

  7. k -> f

  8. k -> c

  9. k -> k

所以總的來說,你有類似的東西

public double convert(double input, Unit from, Unit to) {
    if (from == null || to == null) {
        throw new IllegalArgumentException("units cannot be null");
    }
    switch (from) {
        case FAHRENHEIT:
            switch (to) {
                case FAHRENHEIT:
                    return input;
                case CELSIUS:
                    return (input - 32) * (5D / 9D);
                case KELVIN:
                    return (input + 459.67) * (5D / 9D);
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        case CELSIUS:
            switch (to) {
                case FAHRENHEIT:
                    return input * (9D / 5D) + 32;
                case CELSIUS:
                    return input;
                case KELVIN:
                    return input + 273.5;
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        case KELVIN:
            switch (to) {
                case FAHRENHEIT:
                    return (input - 273.5) * (9D / 5D) + 32;
                case CELSIUS:
                    return input - 273.5;
                case KELVIN:
                    return input;
                default:
                    throw new IllegalStateException("Unhandled from / to values: " + from + " " + to);
            }
        default:
            throw new IllegalStateException("Unhandled from value: " + from);
    }

}

如果需要,您可以根據輸入重構為更小的方法,以減少 switch 語句中的分支,從而稍微整理一下。

如果您使用的是 java 13 或更高版本,另一種方法是使用增強型開關。 此時, convert 方法將如下所示:

public double convert(double input, Unit from, Unit to) {
    if (from == null || to == null) {
        throw new IllegalArgumentException("units cannot be null");
    }
    return switch (from) {
        case FAHRENHEIT -> switch (to) {
            case FAHRENHEIT -> input;
            case CELSIUS -> (input - 32) * (5D / 9D);
            case KELVIN -> (input + 459.67) * (5D / 9D);
        };
        case CELSIUS -> switch (to) {
            case FAHRENHEIT -> input * (9D / 5D) + 32;
            case CELSIUS -> input;
            case KELVIN -> input + 273.5;
        };
        case KELVIN -> switch (to) {
            case FAHRENHEIT -> (input - 273.5) * (9D / 5D) + 32;
            case CELSIUS -> input - 273.5;
            case KELVIN -> input;
        };
    };

看到這里,我認為它更具可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM