循环输入商品编号和购买数量,系统自动计算每种商品的价钱(单价*购买数量),并累计到总金额。 当用户输入“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(“程序结束!”);
}
}

原文链接:https://blog.csdn.net/qq_45671329/article/details/102563486?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165277499316781432921534%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165277499316781432921534&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-26-102563486-null-null.nonecase&utm_term=%E4%BC%98%E6%83%A0

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
文明发言,共建和谐米科社区
提交
头像

昵称

取消
昵称表情图片