素语697 2022-12-26 15:26 采纳率: 100%
浏览 26
已结题

java语言实验第二个应该怎么弄?

img


java语言实验第二个应该怎么弄?
是不是应该用for循环语句?

  • 写回答

3条回答 默认 最新

  • ShowMeAI 2022-12-26 16:12
    关注

    详细计算超市购物小票上的折扣和总金额的Java代码和解析如下,望采纳,有问题可以交流

    • 首先,需要定义一个商品类,包含商品名称、单价和数量等信息:
    public class Item {
        private String name;
        private double price;
        private int quantity;
    
        public Item(String name, double price, int quantity) {
            this.name = name;
            this.price = price;
            this.quantity = quantity;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public int getQuantity() {
            return quantity;
        }
    
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
    }
    
    • 然后,可以定义一个超市购物小票类,包含购买的商品列表、总金额、折扣和实际支付的金额:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Receipt {
        private List<Item> items;
        private double totalAmount;
        private double discount;
        private double actualAmount;
    
        public Receipt() {
            this.items = new ArrayList<>();
            this.totalAmount = 0;
            this.discount = 0;
            this.actualAmount = 0;
        }
    
        public void addItem(Item item) {
            this.items.add(item);
            this.totalAmount += item.getPrice() * item.getQuantity();
        }
    
        public void setDiscount(double discount) {
            this.discount = discount;
            this.actualAmount = this.totalAmount * (1 - discount);
        }
    
        public List<Item> getItems() {
            return items;
        }
    
        public double getTotalAmount() {
            return totalAmount;
        }
    
        public double getDiscount() {
            return discount;
        }
    
        public double getActualAmount() {
            return actualAmount;
        }
    }
    
    • 在创建超市购物小票实例之后,你可以通过调用 addItem 方法添加商品,并使用 setDiscount 方法设置折扣。例如:
    Receipt receipt = new Receipt();
    
    // 添加商品
    receipt.addItem(new Item("苹果", 0.99, 10));
    receipt.addItem(new Item("香蕉", 0.59, 20));
    receipt.addItem(new Item("橙子", 0.79, 5));
    
    // 设置折扣
    receipt.setDiscount(0.1);
    
    // 打印小票
    System.out.println("总金额: " + receipt.getTotalAmount());
    System.out.println("优惠折扣: " + receipt.getDiscount());
    System.out.println("实际金额: " + receipt.getActualAmount());
    

    执行上面的代码后,将输出以下信息:

    总金额: 75.6
    优惠折扣: 0.1
    实际金额: 68.04
    

    你还可以使用循环遍历购物小票中的商品列表,并打印出每件商品的名称、单价和数量等信息:

    System.out.println("Item list:");
    for (Item item : receipt.getItems()) {
        System.out.println(item.getName() + " - " + item.getPrice() + " x " + item.getQuantity());
    }
    

    执行上面的代码后,将输出以下信息:

    Item list:
    苹果 - 0.99 x 10
    香蕉 - 0.59 x 20
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月26日

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)