循环输入商品编号和购买数量,系统自动计算每种商品的价钱(单价*购买数量),并累计到总金额。 当用户输入“n”时,表示需要结账,则退出循环开始结账 结账时,根据折扣(假设享受八折优惠)计算应付金额

/java理解程序逻辑8.0 P107上级练习三
循环输入商品编号和购买数量,系统自动计算每种商品的价钱(单价
购买数量),并累计到总金额。
当用户输入“n”时,表示需要结账,则退出循环开始结账
结账时,根据折扣(假设享受八折优惠)计算应付金额,输入实付金额,计算找零。/
import java.util.Scanner;
public class PriceLookup {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
String answer;
double sum = 0;
double price;//price 价格
int count;//count 个数
double discount = 0.8;
System.out.println(“MyShopping购物管理系统>>购物结算”);
System.out.println("\n*****************************");
System.out.println(“请选择购买的商品编号:”);
System.out.println(“1.T恤\t2.网球鞋\t3.网球拍”);
System.out.println("
");
System.out.println(“请输入商品编号:”);
number = input.nextInt();
System.out.println(“请输入购买数量:”);
count = input.nextInt();
switch (number) {
case 1:
price = 245;
System.out.println(“T恤\t”+price+"\t\t数量"+count+"\t\t合计"+price
count);
sum = sum+price
count;
break;
case 2:
price = 570;
System.out.println(“网球鞋\t”+price);
sum = sum+price
count;
break;
case 3:
price = 320;
System.out.println(“网球拍\t”+price);
sum = sum+price
count;
break;
default:
System.out.println(“输入错误!”);
break;
}
System.out.println("\n是否继续(y/n)?;
answer = input.next();
while(answer.equals(“y”)){
System.out.println("\n
*****");
System.out.println(“请选择购买的商品编号:”);
System.out.println(“1.T恤\t2.网球鞋\t3.网球拍”);
System.out.println("***********************");
System.out.println(“请输入商品编号:”);
number = input.nextInt();
System.out.println(“请输入购买数量:”);
count = input.nextInt();
switch (number) {
case 1:
price = 245;
System.out.println(“T恤\t”+price+"\t\t数量"+count+"\t\t合计"+price
count);
sum = sum+price
count;
break;
case 2:
price = 570;
System.out.println(“网球鞋\t”+price);
sum = sum+price
count;
break;
case 3:
price = 320;
System.out.println(“网球拍\t”+price);
sum = sum+price
count;
break;
default:
System.out.println(“输入错误!”);
break;
}
System.out.println("\n是否继续(y/n)?;
answer = input.next();
}
System.out.println(“总价格是:sum=”+sum);
System.out.println(“折扣是:discount=”+discount);
double money = sum
discount;
System.out.println(“应付金额是:money=”+money);
System.out.println(“实付金额为:”);
double pay = input.nextDouble();
System.out.println(“找钱:”+(pay-money));
System.out.println(“程序结束!”);
}
}

你可能感兴趣的:(java循环)