【案例2】编写一个智能购物计算小程序

编写一个智能购物计算小程序,在一家商店有书本、铅笔、橡皮、可乐、零食五种商品,商品价格如下。书本12元,铅笔,1元;橡皮,2元;可乐,3元;零食,5元。假如小明带了20元,且必须购买一本书,剩余的钱还可以购买哪种商品,可以购买几件,购买完后又能剩余多少钱?(提示:买一本书后,剩余的钱可以有多种处理方式,请采用选择结构解决问题)

样例:

 

package lesson_02;
import java.util.Scanner;
public class shopping {
    public static void main(String[] args) {
        int pencil=1;//铅笔价格
        int rubber=2;//橡皮价格
        int cola=3;//可乐价格
        int book=12;//书价格
        int snacks=5;//零食价格
        int spaceship=10000000;
        System.out.println("书本的价格为"+book+"元,您共有20元");
        System.out.println("1.铅笔的价格为"+pencil+"元");
        System.out.println("2.橡皮的价格为"+rubber+"元");
        System.out.println("3.可乐的价格为"+cola+"元");
        System.out.println("4.零食的价格为"+snacks+"元");
        System.out.println("5.飞船的价格为"+spaceship+"元");
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入其他需要购买商品的名称:");
        int id=sc1.nextInt();
        switch(id) {
            case 1:
                int pencilmoney=20-book;
                int pencilsum=pencilmoney/pencil;
                int pencilsurplus=pencilmoney%pencil;
                System.out.println("购买完书本后还可以购买铅笔"+pencilsum+"个,还剩"+pencilsurplus+"元");
                break;
            case 2:
                int rubbermoney=20-book;
                int rubbersum=rubbermoney/rubber;
                int rubbersurplus=rubbermoney%rubber;
                System.out.println("购买完书本后还可以购买橡皮"+rubbersum+"个,还剩"+rubbersurplus+"元");
                break;
            case 3:
                int colamoney=20-book;
                int colasum=colamoney/cola;
                int colasurplus=colamoney%cola;
                System.out.println("购买完书本后还可以购买可乐"+colasum+"个,还剩"+colasurplus+"元");
                break;
            case 4:
                int snacksmoney=20-book;
                int snackssum=snacksmoney/snacks;
                int snackssurplus=snacksmoney%snacks;
                System.out.println("购买完书本后还可以购买零食"+snackssum+"个,还剩"+snackssurplus+"元");
                break;
            case 5:
                int spaceshipmoney=20-book;
                int spaceshipsum=spaceshipmoney/spaceship;
                int spaceshipsurplus=spaceshipmoney%spaceship;
                System.out.println("购买完书本后还可以购买飞船"+spaceshipsum+"个,还剩"+spaceshipsurplus+"元");
                break;
            default:
                System.out.println("您输入的有误");
                break;
        }
    }
}

编写这样一个 Java 程序,首先需要创建一个简单的类来表示商品,包括名称、价格和数量等属性。然后创建一个主函数,模拟小明的购物过程。下面是简单的步骤和代码示例: ```java // 商品类 class Item { String name; double price; // 构造函数和 getters/setters 省略... public Item(String name, double price) { this.name = name; this.price = price; } } public class ShoppingCart { private Item book; // 书籍 private Item pencil; private Item eraser; private Item coke; private Item snack; // 初始化商品价格 static final double BOOK_PRICE = 15.0; static final double PENCIL_PRICE = 1.0; static final double ERASER_PRICE = 0.5; static final double COKE_PRICE = 3.0; static final double SNACK_PRICE = 2.0; public void buyBookWith20() { book = new Item("书", BOOK_PRICE); if (20 - book.price >= 0) { System.out.println("小明买了一本书,花费 " + book.price + " 元"); double remainingMoney = 20 - book.price; printOtherItems(remainingMoney); } else { System.out.println("小明的钱不够买一本书"); } } private void printOtherItems(double money) { if (money > 0) { int pencilsToBuy = (int) Math.floor(money / PENCIL_PRICE); System.out.println("还能买 " + pencilsToBuy + " 支铅笔,每支 " + PENCIL_PRICE + " 元"); if (money % PENCIL_PRICE == 0) { // 如果钱正好够买整数支铅笔 System.out.println("剩余 " + (money - pencilsToBuy * PENCIL_PRICE) + " 元"); } else { money -= pencilsToBuy * PENCIL_PRICE; System.out.println("还能买 " + (money / ERASER_PRICE) + " 块橡皮,每块 " + ERASER_PRICE + " 元"); System.out.println("剩余 " + (money - (pencilsToBuy + money / ERASER_PRICE) * ERASER_PRICE) + " 元"); } // 计算其他商品是否能买 // 这里省略了 Coke 和 Snack 的计算,因为它们的价格已知,可以根据剩余金额直接判断 } } // 主函数入口 public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.buyBookWith20(); } } ``` 这个程序会先让小明买一本书,如果还有足够的钱,会根据剩余金额计算能买多少铅笔和橡皮。注意这里的代码没有处理完全精确的货币分配,例如如果剩余的钱刚好只能买半块橡皮,则这里假设无法购买。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值