首页 > 代码库 > switch 找零结账
switch 找零结账
import java.util.Scanner;
public class Pay {
public static void main(String[] args) {
System.out.println("*************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤\t2.网球鞋\t3.网球拍");
System.out.println("*************************");
int goodNo=0;//商品编号
String name="";//产品名称
int amount=0;//购买数量
double yingfu=0.0;//应付
double change=0.0;//找零
double price=0.0; //单价
double discount=0.8;//折扣
double total=0.0;//商品总价
double payment=0.0;//实付金额
Scanner input=new Scanner(System.in);
String answer="y";
while("y".equals(answer)){
System.out.print("请输入商品编号:");
goodNo=input.nextInt();
System.out.print("请输入购买数量:");
amount=input.nextInt();
switch(goodNo){
case 1:
name="T恤";
price=245.0;
break;
case 2:
name="网球鞋";
price=570.0;
break;
case 3:
name="网球拍";
price=320.0;
break;
}
total=price*amount;
System.out.println(name+"\t¥"+price+"\t数量:"+amount+"\t合计:¥"+total);
System.out.print("是否继续(y/n)");
answer=input.next();
}
yingfu=total*discount;
System.out.println("折扣:"+discount);
System.out.println("应付金额:"+yingfu);
System.out.print("实付金额:");
payment=input.nextDouble();
change=payment-yingfu;
System.out.println("找零:"+change);
}
}
switch 找零结账