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 找零結賬