我得到了一个不兼容的类型,需要int found:这一行是void - > moneyCount = countDemoninations(更改,面额[i])。试图理解为什么?
public class Change {
int [] denomination = {1,2,5,10,20,50,100,200,500,1000,2000,5000};
int moneyCount = 0;
public void catagorizeChange(int change){
for (int i = 0; i < denomination.length; i++){
moneyCount = countDemoninations(change, denomination[i]);
}
}
public void countDemoninations(int change, int denomination){
int moneyCount =0;
while (change >= denomination){
moneyCount = moneyCount++;
change = change - denomination;
}
}
}
我是一名新的java学生,我想知道以下编辑的代码是否在一个良好的实践中,即在另一个中使用一种方法同时属于同一个类?
public void countChangedenominations (int change){
for (int i = 0; i < moneyValuearray.length; i++){
moneyCountarray[i] = countDemonination(change, moneyValuearray[i]);
}
}
public int countDemonination(int change, int denomination){
while (change >= denomination){
moneyCount = ++moneyCount;
change = change - denomination;
}
return moneyCount;
答案 0 :(得分:1)
该行问题的根源:
moneyCount = countDemoninations(change, denomination[i]);
countDemoninations
的类型为void,而moneyCount
的类型为int
。
要解决此问题,请将countDemoninations
方法签名更改为返回int
而不是void
,然后返回moneyCount
作为方法中的最后一个语句:
public int countDemoninations( int change, int denomination ) {
int moneyCount = 0;
while ( change >= denomination ) {
moneyCount = moneyCount++;
change = change - denomination;
}
return moneyCount;
}