账单随机生成的java模拟代码
时间: 2024-05-11 21:17:19 浏览: 12
以下是一个简单的随机生成账单的 Java 模拟代码:
```java
import java.util.Random;
public class BillGenerator {
private static final String[] ITEM_NAMES = {"T-shirt", "Jeans", "Sneakers", "Socks", "Hat", "Sunglasses"};
private static final double[] ITEM_PRICES = {19.99, 49.99, 99.99, 9.99, 14.99, 29.99};
private static final int MAX_ITEMS = 10;
public static void main(String[] args) {
Random rand = new Random();
int numItems = rand.nextInt(MAX_ITEMS) + 1;
double[] itemPrices = new double[numItems];
String[] itemNames = new String[numItems];
for (int i = 0; i < numItems; i++) {
int index = rand.nextInt(ITEM_NAMES.length);
itemNames[i] = ITEM_NAMES[index];
itemPrices[i] = ITEM_PRICES[index];
}
double subtotal = 0;
System.out.println("Item\tPrice");
for (int i = 0; i < numItems; i++) {
System.out.println(itemNames[i] + "\t" + itemPrices[i]);
subtotal += itemPrices[i];
}
double tax = subtotal * 0.08;
double total = subtotal + tax;
System.out.println("Subtotal: " + subtotal);
System.out.println("Tax (8%): " + tax);
System.out.println("Total: " + total);
}
}
```
这个程序会随机生成一个账单,包括一些商品和它们的价格,以及总价和税金。你可以根据需要调整 ITEM_NAMES、ITEM_PRICES 和 MAX_ITEMS 的值来改变生成账单的规则。