我为一个项目使用数组来存储货币值,以及一个双变量来保存运行总数。当我通过循环运行我的代码时,用户输入不存储在数组中,并且没有任何内容添加到运行总数中。当用户输入-1时,应该打破循环并计算税金等,并且当输入0时,最后一个值将从数组中移除。无论我做什么,我都无法将这些值存入数组中,或者运行的总数不变。我确信我做错了什么是愚蠢的,但我无法发现它。存储值不起作用?
for(i = 0; i < priceArray.length; i++) {
System.out.print("\nEnter the price of the item...");
userInput = input.nextDouble();
if(userInput == -1) { // This will break the user out of the loop.
break;
}
else if(userInput == 0.0) {
System.out.println("You entered a zero, removing last price of $" + priceArray[i] + ".");
i--;
runningTotal =- priceArray[i];
}
else if(userInput > 0.0 && userInput < 2999.99) {
priceArray[i] = userInput;
priceArray[i] += runningTotal;
userInput += runningTotal;
System.out.println("You entered $" + userInput + ", total is $" + runningTotal + ".");
}
else {
i--;
System.out.println("Please enter a valid value under $2999.99.");
}// End if.
};// End for
得到它运作得不错,谢谢你们。 – WesternFive