题目:
一个超市奖励劵取决于顾客在商品上花费了多少。如下表显示用于计算花费的不同金额的优惠劵的百分比。编写一个程序,在一个人购买商品的基础上计算和打印优惠劵的值。
花费 | 折扣率 |
<$10 | no coupon |
$10~$60 | 8% |
$60~$150 | 10% |
$150~$210 | 12% |
>$210 | 14% |
代码
import java.util.Scanner;
public class coupon
{
private static Scanner in;
public static void main(String[] args)
{
System.out.println("please enter the cost of your goods:");
in = new Scanner(System.in);
double cost = in.nextDouble();
double discountCoupon = 0;
if(cost<=10)
{
System.out.println("no coupon!\n");
}
else if(cost>10 && cost<=60)
{
discountCoupon = cost * 0.08;
}
else if(cost>60 && cost<=150)
{
discountCoupon = cost * 0.1;
}
else if(cost>150 && cost<=210)
{
discountCoupon = cost * 0.12;
}
else
{
discountCoupon= cost * 0.14;
}
System.out.println("You win the discount coupon is:$"+String.format("%3.2f",discountCoupon));
}
}
运行结果: