小编Mor*_*ten的帖子

如何在switch语句中将String转换为枚举?

我有这个代码,我想接受命令行args fx"12 EUR"进行转换:

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) …
Run Code Online (Sandbox Code Playgroud)

java enums switch-statement command-line-arguments

1
推荐指数
1
解决办法
8565
查看次数

在2d数组中打印最大数字 - 为什么我的代码打印三个数字

我想打印出2D数组中最大的数字.我的问题是我的输出是三个数而不是一个 - 最大的.为什么?

这是我的代码:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    int maxRows = 3;
    int maxCols = 4;

    int [] onedArray = new int [maxRows];
        for (int i = 0; i < maxRows; i++){
        onedArray[i] = (int) ((Math.random() * 100) * maxCols);
    }

    int [][] twodArray = new int[maxRows][];
        for (int i = 0; i < maxRows; i++){
        twodArray[i] = new int[maxCols];
    }

        for (int i = …
Run Code Online (Sandbox Code Playgroud)

java numbers max multidimensional-array

1
推荐指数
1
解决办法
3万
查看次数