-2

我试图让我的代码检查某个字符串是否在枚举类型中。这就是我所拥有的:

public enum CurrencyAmt {twenty(20), ten(10), five(5), toonies(2), toony(2), loonies(1), loony(1), quarters(0.25), 
quarter(0.25), dimes(0.1), dime(0.1), nickels(0.05), nickel(0.05), pennies(0.01), penny(0.01);

private int type;
private double value;

CurrencyAmt (int i)
{
    this.type = i;
}

CurrencyAmt (double i)
{
    this.value = i;
}
}

定义我的枚举类型。和表达

(Enum.IsDefined(typeof(CurrencyAmt), inputAt)

检查字符串“inputAt”是否在枚举中。但是,我收到错误 CurrencyAmt 无法解析为变量。有任何想法吗?

4

1 回答 1

2

你需要一个.classafter CurrencyAmt,我认为:

(Enum.IsDefined(typeof(CurrencyAmt.class), inputAt)

但是,由于我无法从 Java 中识别此代码片段的任何其他部分,我认为 Java 版本应该是:

CurrencyAmt.valueOf(inputAt);

...这将返回CurrencyAmt与给定字符串对应的枚举常量,或者如果未定义则抛出异常。

于 2013-10-08T00:21:42.820 回答