它应该继续接受一个项目的价格并在用户输入不等于零时显示当前总价
Posted
技术标签:
【中文标题】它应该继续接受一个项目的价格并在用户输入不等于零时显示当前总价【英文标题】:It should continue to accept a price of an item and displays the current total while the user input is not equals to zero 【发布时间】:2022-01-24 00:17:34 【问题描述】:/* 我需要一些帮助。我被这段代码困住了。当用户输入不等于 0 时,它应该继续接受一个项目的价格并显示当前总价。但是,当前总计的输出不会显示当前金额。 */
public static void main(String[] args)
Scanner scanner = new Scanner(System.in);
String customerName;
char promtSenior;
int num = 1;
double itemPrice;
double currentTotal = 0;
ArrayList<Double> amounts = new ArrayList<Double>();
System.out.print("Please enter customer name: ");
customerName = scanner.nextLine();
while (true)
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts)
currentTotal = currentTotal + i ;
System.out.println(currentTotal);
if (itemPrice == 0)
System.out.println("Are you a Senior Citizen? [Y/N]: " + customerName);
promtSenior = scanner.next().charAt(0);
if (promtSenior == 'N')
System.out.println("No Discount");
【问题讨论】:
【参考方案1】:while (true)
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts)
currentTotal = currentTotal + i ;
System.out.println(currentTotal);
应该像以前一样,在 for 循环之后打印总数而不是在 我认为您不小心弄乱了大括号并在之前缩进了
【讨论】:
以上是关于它应该继续接受一个项目的价格并在用户输入不等于零时显示当前总价的主要内容,如果未能解决你的问题,请参考以下文章