【问题标题】:Argument values error [duplicate]参数值错误[重复]
【发布时间】:2014-04-25 05:27:59
【问题描述】:

对于我的一个课程,我们必须找到一种方法,在给定任意数量的硬币的情况下,用最少的硬币进行一定数量的零钱。下面是我的代码:

public class Changemaker {

public static void main ( String[] args ) {
    int amount = Integer.parseInt(args[args.length-1]);
    int coins = args.length - 1;

    if (amount < 0){
        throw new IllegalArgumentException("IMPROPER AMOUNT"); 

    } else {

        for (int i = 1; i <= coins; i++) {
            int coin = Integer.parseInt(args[i]);

            if (coin <= 0){
                throw new IllegalArgumentException("IMPROPER DENOMINATION"); 

            } else {

                for (int j = 1; j <= coins; j++) {
                    int validCoin = Integer.parseInt(args[j]);

                    if (validCoin == coin && j != i ) {
                        throw new IllegalArgumentException("DUPLICATE DENOMINATION");

                    }
                }
            }

            try { 
                String firstCoin = args[1];

            } catch (ArrayIndexOutOfBoundsException e) { 
                System.out.println("INSUFFICIENT DATA"); 
                throw new ArrayIndexOutOfBoundsException(" ");

            } 
        }
    }



    Tuple [][] table = new Tuple [coins][amount + 1];

    for(int x = 0; x < coins; x++) {
        for (int i = 0; i <= amount; i++) {
            Tuple tuple = new Tuple (coins);
            table[x][i] = tuple;

        }
    }


    for (int i = 1; i <= amount; i++) {
        Tuple tuple = table[0][i];
        int coin = Integer.parseInt(args[1]);
        int total = i;
        int remainder = total % coin;
        int coinAmt= (int)Math.floor(total / coin);

        if (remainder == 0) {
            tuple.setElement(0, coinAmt);
        }

    }


    for(int x = 1; x < coins; x++) {
        int coin = Integer.parseInt(args[x + 1]);

        for (int i = 1; i <= amount; i++) {
            Tuple tuple = table[x][i];
            int total = i;
            int remainder = total % coin;
            int coinAmt= (int)Math.floor(total / coin);

            if (remainder == 0) { 

                tuple.setElement(x, coinAmt);
                int optimalRow = optimalCheck(table, tuple, x, i);

                Tuple optimalTuple = table[optimalRow][i];


                tuple.copy(optimalTuple);

            } else if (remainder != 0 && amount < coin) { 

                tuple.copy(table[x - 1][i]);

            } else if (remainder != 0 && amount > coin) { 

                tuple.setElement(x, coinAmt);
                Tuple remainderTuple = table[x][remainder];
                tuple.add(remainderTuple);

                int optimalRow = optimalCheck(table, tuple, x, i);
                Tuple optimalTuple = table[optimalRow][i];
                tuple.copy(optimalTuple);

            }
        }
    }

    Tuple finalAnswer = table[coins - 1][amount];

    String result = "";
    result = result + finalAnswer.total() + " COIN(S) IN TOTAL: ";

    for (int i = 0; i < coins; i++) {
        result = result + finalAnswer.getElement(i) + " x " + args[i + 1] + " cent , ";

    }


    if (finalAnswer.total() == 0) {
        System.out.println("AMOUNT CANNOT BE MADE");

    } else {
        System.out.println(result);

    }

}



public static int optimalCheck(Tuple[][] table, Tuple tuple, int row, int column) {
    int total = tuple.total();
    int optimalRow = 0;

    for (int i = 0; i <= row; i++ ){
        int checkedTotal = table[i][column].total();    

        if (checkedTotal < total && checkedTotal > 0) {
            optimalRow = optimalRow + i;
            break;
        } else if (checkedTotal == total && i == row) {
            optimalRow = optimalRow + row;
            break;
        }
    }

    return optimalRow;
} }

在大多数情况下,我的代码是正确的。唯一的问题是,它将要进行的更改数量计算为面额,并且会减少我的第一个面额金额。

因此,例如,如果我在命令行中输入 1 5 10 25 133(1、5、10 和 25 美分硬币可以得到 133 美分),它会返回:

1 COIN(S) IN TOTAL: 0 x 5 cent , 0 x 10 cent , 0 x 25 cent , 1 x 133 cent ,

我查看了代码,但没有看到哪里出错了。谁能告诉我我在哪里犯了错误?非常感谢。

【问题讨论】:

  • 与问题没有直接关系,但你有amount &lt; 0,但你永远不会有负数的论点。你可能想要amount &lt;= 0 或者只是amount == 0
  • Tuple类的定义在哪里?
  • 我的 Tuple 类在一个单独的 .java 文件中,但在同一个源文件夹中。
  • 对不起,我应该更具体一点:你能发布你的 Tuple 类的代码吗?

标签: java command-line-arguments


【解决方案1】:

在你的 for 循环中,你想要

for (int i = 1; i < coins; i++) { 

不是

for (int i = 1; i <= coins; i++) {

&lt;= 将包含coins,即length - 1 - 最后一个参数。

【讨论】:

  • 我试过了,结果还是一样。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 2021-02-07
  • 1970-01-01
相关资源
最近更新 更多