繁体   English   中英

类型的未定义方法

[英]Undefined method for a type

我正在尝试实现一个程序,该程序计算从硬币列表中取出特定数量的可能性的数量,但出现错误

在钱币(s-1)的这一行中,对于类型为Money的类型,未定义钱币(int)方法:
返回梳子(s-1,数量,硬币)+梳子(s,数量-硬币(s-1),硬币);

这是我的代码

class List<T> {
T head;
List<T> tail;

List(T head, List<T> tail) {
    this.head = head;
    this.tail = tail;
}
static <U> List<U> node(U head, List<U> tail) {
    return new List<U>(head, tail);
}
}

public class Money{

//should count number of combinations to get change amount amount

static int comb(int s, int amount, List<Integer> coins) { 
         if (amount == 0 ) 
                return 1;

     else if (amount < 0) 
            return 0;

     return comb(s-1, amount, coins) + comb(s, amount-coins(s-1), coins); 

问题是什么?

我认为您的问题是您如何尝试访问列表的元素。

return comb(s-1, amount, coins) + comb(s, amount-coins(s-1), coins); 

应该:

return comb(s-1, amount, coins) + comb(s, amount-coins.get(s-1), coins);

Coins是一个列表,因此您应该使用coin.get(index)访问单个元素。 是有关Java中的列表的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM