嵌套加深练习题: 代码量较大所以直接copy过来就不发图了(抱歉) import java.util.Scanner;//导包 public class TextShopping{ public static void main(String[] args){ System.out.println("***********************************************"); System.out.println("请选择购买的商品编号:"); System.out.println("1.T恤t2.网球鞋t3.网球拍"); System.out.println("***********************************************"); Scanner sc = new Scanner(System.in); String flag = "y"; //存储商品编号的变量 int pId = 0; //存储购买数量的变量 int num = 0; //T恤的单价 double tPrice = 245.0; //网球鞋的单价 double wqxPrice = 570.0; //网球拍的单价 double pPrice = 320.0; //每次购买商品的总价; double oneTotalPrice = 0; //定义一个计算购买商品总额的变量 double totalPrice = 0; while("y".equals(flag)){ System.out.println(); System.out.println("请输入商品编号:"); pId = sc.nextInt(); System.out.println("请输入购买数量:"); num = sc.nextInt(); switch(pId){ case 1: oneTotalPrice = tPrice*num; System.out.println("T恤 ¥"+tPrice+"t"+"数量 "+num+"t"+"合计 ¥"+oneTotalPrice); totalPrice += oneTotalPrice; oneTotalPrice = 0; break; case 2: oneTotalPrice = wqxPrice*num; System.out.println("网球鞋 ¥"+wqxPrice+"t"+"数量 "+num+"t"+"合计 ¥"+oneTotalPrice); totalPrice += oneTotalPrice; oneTotalPrice = 0; break; case 3: oneTotalPrice = pPrice*num; System.out.println("网球拍 ¥"+pPrice+"t"+"数量 "+num+"t"+"合计 ¥"+oneTotalPrice); totalPrice += oneTotalPrice; oneTotalPrice = 0; break; } System.out.println("y/n"); flag = sc.next(); } System.out.println(); System.out.print("折扣:"); //定义折扣变量 double zk = sc.nextDouble(); totalPrice *= zk; System.out.println("应付金额:" + totalPrice); //定义实付变量 System.out.print("实付金额:"); double sf = sc.nextDouble(); if(sf < totalPrice){ System.out.print("您输入的金额小于应付金额,请重新输入:"); sf = sc.nextDouble(); } System.out.println("找钱:"+ (sf-totalPrice)); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)