该程序向用户询问账单,然后他们支付了多少钱,并且应该计算并打印出零钱。我离开了第二部分,因为我遇到的问题是计算第二个 for 循环中的变化。根据用户输入,发生的错误java.lang.NullPointerException
在不同的硬币、四分之一、镍线等。我已经查找了问题,人们说它的程序试图获取一个值,null
但我不知道如何,因为我正在更新。
在此先感谢您的帮助
Scanner s = new Scanner(System.in);
Double[] Bill = new Double[5];
Double[] Paid = new Double[5];
for (int i = 0; i < 1; i++) {
int j = i + 1;
System.out.print("Enter bill " + j + ": ");
Bill[i] = Double.parseDouble(s.nextLine());
System.out.print("Enter amount paid for bill " + j + ": ");
Paid[i] = Double.parseDouble(s.nextLine())
System.out.println("");
}
Integer[] Dollars = new Integer[5];
Integer[] Quarters = new Integer[5];
Integer[] Dimes = new Integer[5];
Integer[] Nickels = new Integer[5];
Integer[] Pennies = new Integer[5];
for (int i = 0; i < 1; i++){
Double Change = Paid[i] - Bill[i];
while (Change > 0){
if (Change > 1){
Change = Change - 1;
Dollars[i] = Dollars[i] + 1;
}
else if (Change > 0.25){
Change = Change - 0.25;
Quarters[i] = Quarters[i] + 1;
}
else if (Change > .1){
Change = Change - 0.1;
Dimes[i] = Dimes[i] + 1;
}
else if (Change > .05){
Change = Change - 0.05;
Nickels[i] = Nickels[i] + 1;
}
else if (Change > 0){
Change = Change = 0.01;
Pennies[i] = Pennies[i] + 1;
}
}
}