我正在使用贪婪算法来计算变化,我想将我用过的硬币保存到新的aray中,但是我:
Exception in thread "main" java.lang.NullPointerException
at lacy_lab_7b_2015.Lacy_Lab_7b_2015.main(Lacy_Lab_7b_2015.java:36)
Java Result: 1
来自编译器的。
public class Lacy_Lab_7b_2015 {
public static Scanner input = new Scanner(System.in);// scanner object to allow user input
public static int amount;// global variable defined by user to be used to have certain value of coins added so that they wll equal this value
public static int count = 0;
final public static int[] FACE_VALUES_OF_COINS = {100, 50, 25, 10, 5, 1};
public static int[] coinsUsed;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
amount = userAmount();
// coinsUsed = coinsUsed(FACE_VALUES_OF_COINS,amount);
for (int i = 0; i < FACE_VALUES_OF_COINS.length; i++) {
int j = 0;
if (FACE_VALUES_OF_COINS[i] < amount) {
int count = amount / FACE_VALUES_OF_COINS[i];
coinsUsed[j] = FACE_VALUES_OF_COINS[i];
amount -= count * FACE_VALUES_OF_COINS[i];
}//end if
}//end for
for (int i = 0; i < coinsUsed.length; i++) {
System.out.println(coinsUsed[i]);
}
}//end main
答案 0 :(得分:1)
我建议你写一下
public static int [] coinsUsed;
作为
public static int [] coinsUsed = new int [5];