购物结算JAVA的代码
import java.util.Scanner;
public class TestShopping{
public static void main(String[] args){
System.out.println("***************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤t2.网球鞋t3.网球拍");
System.out.println("***************************************");
//创建Scanner对象
Scanner sc = new Scanner(System.in);
String flag = "y";
//存储商品编号的变量
int pId = 0;
//存储商品数量的变量
int num = 0;
//T恤的单价
double tPrice = 245.0;
//网球鞋的单价
double sPrice = 570.0;
//网球拍的单价
double pPrice = 320.0;
//定义每次购买商品总价
double oneTotalPrice = 0;
//定义一个计算购买商品总额的变量
double totalPrice = 0;
while("y".equals(flag)){
System.out.println();
System.out.print("请输入商品编号:");
pId = sc.nextInt();
System.out.print("请输入购买数量:");
num = sc.nextInt();
switch(pId){
case 1:
oneTotalPrice = tPrice * num;
System.out.println("T 恤¥" + tPrice + "t" + "数量 " + num + "t" + "合计 " + "¥" + oneTotalPrice);
totalPrice +=oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
case 2:
oneTotalPrice = sPrice * num;
System.out.println("网球鞋¥" + sPrice + "t" + "数量 " + num + "t" + "合计 " + "¥" + oneTotalPrice);
totalPrice +=oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
case 3:
oneTotalPrice = pPrice * num;
System.out.println("网球拍¥" + pPrice + "t" + "数量 " + num + "t" + "合计 " + "¥" + oneTotalPrice);
totalPrice +=oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
}
System.out.print("是否继续(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条)