如何在switch语句中将String转换为enum? [英] How do I convert String to enum for at switch statement?

查看:186
本文介绍了如何在switch语句中将String转换为enum?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public主要类别{

枚数货币{EUR,USD,GBP,INVALID_CURRENCY;
static final float C_EUR_TO_DKK_RATE =(float)7.44;
static final float C_USD_TO_DKK_RATE =(float)5.11;
static final float C_GBP_TO_DKK_RATE =(float)8.44;
static float result = 0;
static int amount = 0;
/ **
* @param args命令行参数
* /
public static void main(String [] args){
// Q1

if(args.length == 2){
amount = Integer.parseInt(args [0]);
String currencyIn = args [1] .toString();

货币enumConversion = currencyIn; // **< ---- HERE **
switch(enumConversion){
case EUR:
result = amount * C_EUR_TO_DKK_RATE;
break;
case USD:
result = amount * C_USD_TO_DKK_RATE;
break;
case GBP:
result = amount * C_GBP_TO_DKK_RATE;
break;
默认值:
result = 0;
break;
}
System.out.println((float)amount ++ enumConversion +转换为
+ Math.round(result * 100.0)/100.0 +DKK);


} else {
System.out.println(Invalid arguments!);
System.exit(1);
}
}

}
}

如何将String currencyIn转换为枚举,以便我可以在switch语句中使用args输入?

解决方案

javadocs ,有一种方法这可以用来做到这一点。

  static< T extends Enum< T>> T valueOf(类< T>枚举类型,字符串名称)

ie

 货币enumConversion =货币.valueOf(currencyIn); // **< ----这里** 

作为一个随机的笔记,我几乎总是为我的枚举添加一个 iValueOf (即不区分大小写的方法),以方便使用。


I have this code where I want to accept command line args fx "12 EUR" to do a conversion:

public class Main {

   enum Currency {EUR, USD, GBP,INVALID_CURRENCY;
   static final float C_EUR_TO_DKK_RATE = (float) 7.44;
   static final float C_USD_TO_DKK_RATE = (float) 5.11;
   static final float C_GBP_TO_DKK_RATE = (float) 8.44;
   static float result = 0;
   static int amount = 0;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Q1

        if (args.length == 2) {
            amount = Integer.parseInt(args[0]);
            String currencyIn = args[1].toString();

            Currency enumConversion = currencyIn; //**<---- HERE**
            switch (enumConversion) {
                case EUR:
                    result = amount * C_EUR_TO_DKK_RATE;
                    break;
                case USD:
                    result = amount * C_USD_TO_DKK_RATE;
                    break;
                case GBP:
                    result = amount * C_GBP_TO_DKK_RATE;
                    break;
                default:
                    result = 0;
                    break;
            }
            System.out.println((float) amount + " " + enumConversion + " converts to "
                + Math.round(result*100.0)/100.0 + " DKK");


        } else {
            System.out.println("Invalid arguments!");
            System.exit(1);
        }
    }

}
   }

How do I convert the String currencyIn to enum so I can use the args input in the switch statement?

解决方案

From the javadocs, there is a method that can be used to do this.

static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 

ie

Currency enumConversion = Currency .valueOf(currencyIn); //**<---- HERE**

As a random side note, I almost always add an iValueOf (ie, a case insensitive version) method to my enums, for ease of use.

这篇关于如何在switch语句中将String转换为enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆