首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果您的值不是固定的,如何使用Enum?

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

Stack Overflow用户
提问于 2021-11-09 21:41:01
回答 1查看 54关注 0票数 1

我知道我的问题不是很清楚,但我在这里尝试做的是使用枚举来转换温度单位,我只知道枚举有一个固定值,例如,从华氏度转换到摄氏度需要计算工作所需的输入,但我不能使用主活动中的输入,所以我尝试使用其设置器声明输入,并使用类Convert2在主活动中设置输入(类似于c.setInput( input )),并获取它的值以便能够在类中使用它,我认为如果获得值,就可以将其相加在常量中,然后使末尾的返回值等于1乘以乘数,乘数=为常量,但这不起作用,我被困在这里,因为我不熟悉枚举。希望你能理解我在代码中的意思。

代码语言:javascript
复制
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///
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-09 22:35:06

您可以了解一下使用Switch语句。例如

如果您按如下方式定义单元枚举

代码语言:javascript
复制
enum Unit {
    FAHRENHEIT,
    CELSIUS,
    KELVIN
}

一个完整的“转换”方法将对所有情况求值:

  1. f -> f

  1. f -> c

  1. f -> k

  1. c -> f

  1. c -> c

  1. c -> k

  1. k -> f

  1. k -> c

  1. k -> k

所以你有一些像这样的东西

代码语言:javascript
复制
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或更高版本,另一种方法是使用增强的开关。在这一点上,转换方法将如下所示:

代码语言:javascript
复制
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;
        };
    };

here,我觉得它的可读性更好。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69905462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档