用Java编写一个智能购物计算小程序,在一家商店有书本、铅笔、橡皮、可乐、零食五种商品,商品价格
时间: 2023-03-30 13:02:21 浏览: 479
我可以回答这个问题。以下是一个用Java编写的智能购物计算小程序:
import java.util.Scanner;
public class ShoppingCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double bookPrice = 10.;
double pencilPrice = 2.;
double eraserPrice = 1.;
double cokePrice = 3.;
double snackPrice = 5.;
int bookQty, pencilQty, eraserQty, cokeQty, snackQty;
double totalPrice;
System.out.println("欢迎使用智能购物计算小程序!");
System.out.println("以下是我们商店的商品清单:");
System.out.println("1. 书本,价格:" + bookPrice + "元");
System.out.println("2. 铅笔,价格:" + pencilPrice + "元");
System.out.println("3. 橡皮,价格:" + eraserPrice + "元");
System.out.println("4. 可乐,价格:" + cokePrice + "元");
System.out.println("5. 零食,价格:" + snackPrice + "元");
System.out.print("请输入您要购买的书本数量:");
bookQty = input.nextInt();
System.out.print("请输入您要购买的铅笔数量:");
pencilQty = input.nextInt();
System.out.print("请输入您要购买的橡皮数量:");
eraserQty = input.nextInt();
System.out.print("请输入您要购买的可乐数量:");
cokeQty = input.nextInt();
System.out.print("请输入您要购买的零食数量:");
snackQty = input.nextInt();
totalPrice = bookPrice * bookQty + pencilPrice * pencilQty + eraserPrice * eraserQty + cokePrice * cokeQty + snackPrice * snackQty;
System.out.println("您购买的商品总价为:" + totalPrice + "元。");
}
}
希望这个程序能够帮助您计算购物总价。
阅读全文