【发布时间】:2021-12-22 14:11:04
【问题描述】:
我知道我的问题并不清楚,但我在这里尝试做的是使用枚举来转换温度单位,我只知道枚举具有固定值,因此例如从华氏温度转换为摄氏温度需要输入计算工作,但我不能使用主活动中的输入,所以我尝试用它的设置器声明一个输入并使用类 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///
}
}
【问题讨论】:
标签: java android android-studio class enums