タスクの完了に非常に近づいていますが、findChange()
正しく呼び出す方法がわかりません。私の推測では、それはメインメソッドにある必要があります。しかし、findChange(); の場合。それを呼び出すと、int, List<Integer>, List<Integer>
いわば「正しく」これを行うにはどうすればよいかを尋ねられます。
コード
import java.io.*;
import java.util.*;
import java.lang.*;
public class homework5 {
public static int change;
public static void main(String[] args)
throws FileNotFoundException
{ //begin main
ArrayList<Integer> coinTypes = new ArrayList<Integer>();//array to store
//coin types
Integer i;
File f = new File (args[0]);
Scanner input = new Scanner(f); //initialize scanner
input.nextLine();
while(input.hasNextInt()) {
i = input.nextInt();
coinTypes.add(i);
}
change = coinTypes.get(coinTypes.size()-1); //this will add all ints
coinTypes.remove(coinTypes.size()-1);
System.out.println("Found change"); //used for debugging
System.out.println("Change: " + change);
//findChange(); ideal spot to call the method
//System.out.println(coinTypes);
}
boolean findChange(int change, List<Integer> coinTypes,
List<Integer> answerCoins)
{ //contains means of
//finding the change solutions
if(change == 0) {
return true; //a solution
}
if(change < 0) {
return false; //if negative it can't be a solution
} else {
for(Integer coin : coinTypes) {
if(findChange(change - coin, coinTypes, answerCoins)){
answerCoins.add(coin); //if it works out add it to the
return true; //solution List
}
}
}
List<Integer> answer = new ArrayList<Integer>();
boolean canFindChange = findChange(change, coinTypes, answer);
if(canFindChange) { //if there is a solution, print it
System.out.println(answer);
} else { System.out.println("No change found");
}
return false; //else return false
}
}
このプログラムは、特定の金額、つまり 143 ($1.43) の変化を示すさまざまな方法をすべて計算します。私がしなければならないのはfindChange()
、メインを呼び出すことだけです。それは機能するはずです。何が欠けていますか?
EDITメソッド呼び出しを指定していないことに気づきました。助けが必要です。不明な点がありましたらお詫び申し上げます
入力ファイル
// Coins available in the USA, given in cents. Change for $0.09?
1 5
9
電流出力
Change: 9
欲しいです
Change: 9
['solutions to all possible combinations to make $0.09']