用Java编写 需求说明 输入三种商品的价格和购买数量,打印消费凭据算输入折扣,并计算折后价前时用户支付1500元,打印购物小票。 支付完毕后,显示此次购物获得的积分(每消费100元可获得1元,不足100元,不予以计算)。 double discount;//折扣 int bonus;//抵扣金 System.out.println("输入折扣(0-1区间数)"); discount= input.nextDouble(); System.out.println("折后价:\t"+total*discount); System.out.println("扫描二维码支付中,请等待...\n支付完毕!"); bonus= (int) ((total*discount/100)*1);
时间: 2024-02-18 13:03:49 浏览: 25
好的,我来解答你的问题。你的问题是如何用Java编写一个消费凭据并计算折后价和积分。下面是一个简单的实现:
```java
import java.util.Scanner;
public class Shopping {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 输入商品1的价格和数量
System.out.println("请输入商品1的价格:");
double price1 = input.nextDouble();
System.out.println("请输入商品1的数量:");
int quantity1 = input.nextInt();
// 输入商品2的价格和数量
System.out.println("请输入商品2的价格:");
double price2 = input.nextDouble();
System.out.println("请输入商品2的数量:");
int quantity2 = input.nextInt();
// 输入商品3的价格和数量
System.out.println("请输入商品3的价格:");
double price3 = input.nextDouble();
System.out.println("请输入商品3的数量:");
int quantity3 = input.nextInt();
// 计算总价
double total = price1 * quantity1 + price2 * quantity2 + price3 * quantity3;
// 计算折扣和折后价
System.out.println("请输入折扣(0-1之间的小数):");
double discount = input.nextDouble();
double discountedTotal = total * discount;
System.out.println("折后价为:" + discountedTotal);
// 模拟支付过程
System.out.println("支付中,请稍等...");
while (true) {
try {
Thread.sleep(1000); // 模拟支付延迟
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("支付中,请稍等...");
if (Math.random() < 0.9) {
break; // 90%的概率支付成功
}
}
System.out.println("支付成功!");
// 计算积分
int bonus = (int) (discountedTotal / 100);
System.out.println("获得的积分为:" + bonus);
// 打印小票
System.out.println("**********购物小票**********");
System.out.println("商品\t数量\t单价\t小计");
System.out.println("商品1\t" + quantity1 + "\t" + price1 + "\t" + price1 * quantity1);
System.out.println("商品2\t" + quantity2 + "\t" + price2 + "\t" + price2 * quantity2);
System.out.println("商品3\t" + quantity3 + "\t" + price3 + "\t" + price3 * quantity3);
System.out.println("总计\t\t\t\t" + total);
System.out.println("折扣\t\t\t\t" + discount);
System.out.println("折后价\t\t\t\t" + discountedTotal);
System.out.println("获得积分\t\t\t" + bonus);
}
}
```
这个程序会提示用户依次输入三种商品的价格和数量,并且计算总价。然后用户可以输入一个折扣,程序会计算折后价并模拟支付过程。最后程序会计算获得的积分并打印小票。注意,这个程序只是一个简单的实现,实际场景中可能需要考虑更多细节,例如商品信息存储、支付方式选择等。希望能够对你有所帮助!