要JAVA编写一个程序,购物不足50元的按原价付款,超过50不足100的,超过的部分按九折付款,超过100的,超过部分按八折付款,100以内的按九折付款,统一编程实现主动续费的工作
时间: 2023-08-02 09:10:28 浏览: 52
下面是一个简单的JAVA程序,实现了你所描述的购物支付功能,同时添加了实现主动续费的功能。你可以参考这个程序的逻辑和实现方式,根据自己的需求进行修改。
```java
import java.util.Scanner;
public class ShoppingPayment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double totalPrice = 0;
double discountPrice = 0;
double renewFee = 0;
boolean isRenew = false;
// 输入商品价格,计算总价
System.out.print("请输入商品价格(输入0结束):");
double price = input.nextDouble();
while (price != 0) {
totalPrice += price;
System.out.print("请输入商品价格(输入0结束):");
price = input.nextDouble();
}
// 根据总价计算折扣价格
if (totalPrice < 50) {
discountPrice = totalPrice;
} else if (totalPrice < 100) {
discountPrice = totalPrice * 0.9;
} else {
discountPrice = 100 * 0.9 + (totalPrice - 100) * 0.8;
}
// 输出折扣价格
System.out.println("折扣价格为:" + discountPrice);
// 判断是否需要续费
if (discountPrice < 50) {
isRenew = true;
renewFee = 50 - discountPrice;
}
// 输出续费信息
if (isRenew) {
System.out.println("需要续费,续费金额为:" + renewFee);
} else {
System.out.println("不需要续费");
}
}
}
```
程序中使用了Scanner类来实现用户输入商品价格的功能,使用循环来累加所有商品的价格计算总价。然后根据总价计算折扣价格,判断是否需要续费,并输出相应的信息。