gpt4 book ai didi

java - 如何将数组的内容设置为等于第二个数组的内容

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:16 24 4
gpt4 key购买 nike

我正在使用贪婪算法来计算找零,我想将我用过的一组硬币保存到一个新的数组中,但是我:

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

最佳答案

我建议你写

public static int[] coinUsed;

作为

public static int[] coinUsed = new int[5];

关于java - 如何将数组的内容设置为等于第二个数组的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29158993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com