【发布时间】:2015-03-08 03:07:11
【问题描述】:
此代码是收据程序的一部分。我循环它,以便用户可以输入商品价格(最多 20 件商品)。我需要打印所有项目价格的总和。请注意,所有商品价格都存储在同一个双变量 newItemPrice 中。这甚至可能吗?如果没有,请给我一个关于另一种方法的想法。
while(x < 20){//maximum of 20 items
x++;//item # (x was decalred as an integer of 1)
System.out.println("\nEnter new item's price");
Scanner newItemPriceSC = new Scanner(System.in);
Double newItemPrice = newItemPriceSC.nextDouble();//scans next double (new item's price)
System.out.println("ITEM # " + x + "\t" + "$" + newItemPrice);//item prices
System.out.println("\n");
System.out.println("type \"no more!\" if there are no more items\ntype any other word to continue");
Scanner continueEnd = new Scanner(System.in);
String answ = continueEnd.nextLine();
if(!(answ.equals("no more!"))){
continue;
}
if(answ.equals("no more!")){
break;//ends loop
}
break;//ends loop (first break; was for a loop inside of this loop)
【问题讨论】:
标签: java loops while-loop sum add