Java 简易售卖系统
时间: 2024-03-07 07:25:26 浏览: 16
以下是一个简单的 Java 售卖系统的示例代码:
```
import java.util.Scanner;
public class SalesSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double price;
int quantity;
double total = 0;
char choice;
do {
System.out.print("Enter unit price: ");
price = sc.nextDouble();
System.out.print("Enter quantity: ");
quantity = sc.nextInt();
total += price * quantity;
System.out.print("Add more items? (Y/N): ");
choice = sc.next().charAt(0);
} while (choice == 'Y' || choice == 'y');
System.out.println("Total price: $" + total);
}
}
```
此程序会要求用户输入商品单价和数量,然后计算出总价钱。如果用户想要输入更多商品,程序会继续进行循环,直到用户输入 "N" 或 "n"。最后,程序会输出总价钱。