Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数组的内容设置为第二个数组的内容_Java_Arrays_Nullpointerexception_Greedy - Fatal编程技术网

Java 如何将数组的内容设置为第二个数组的内容

Java 如何将数组的内容设置为第二个数组的内容,java,arrays,nullpointerexception,greedy,Java,Arrays,Nullpointerexception,Greedy,我正在使用贪婪算法计算变化,我想将我使用的一组硬币保存到一个新的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 Scanne

我正在使用贪婪算法计算变化,我想将我使用的一组硬币保存到一个新的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
公共级花边实验室7b_2015{
公共静态扫描仪输入=新扫描仪(System.in);//允许用户输入的扫描仪对象
public static int amount;//由用户定义的全局变量,用于添加硬币的特定值,以便它们将等于此值
公共静态整数计数=0;

最终公共静态整数[]面值硬币的面值={100,50,25,10,5,1}; 公共静态int[]共使用; /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ //此处的TODO代码应用程序逻辑 amount=userAmount(); //coinsUsed=coinsUsed(面值、金额); 对于(inti=0;i
我建议你写

公共静态int[]共使用

作为


公共静态int[]coinsUsed=new int[5];

36 amount-=count*FACE_VALUES_OF_COINS[i];但在我的第35行,那一行是:coinsUsed[j]=FACE_VALUES_OF_COINS[i];你在哪里定义
FACE_VALUES_OF_COINS
?你是否声明并分配了coinsUsed数组最终公共静态int[]FACE_VALUES_OF_COINS={100, 50, 25, 10, 5, 1};