You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#控制台交易总价计算程序:多输入支持与Scene卡折扣功能修复

C#控制台交易总价计算程序修复方案

原程序问题

  • 仅支持单个商品计算,无法处理多商品批量输入与汇总
  • Scene卡(原代码误写为Scena)折扣逻辑错误:非牛奶类商品错误地按1折计算(原代码basePrice *= 0.10),不符合“减免10%”的需求;牛奶类的折扣逻辑也存在顺序问题

需求说明

  • 支持用户输入多件商品信息并完成批量结算
  • 正确实现Scene卡折扣规则:
    • 持Scene卡时,苹果、辣椒、番茄的单价减免10%(即原价×0.9)
    • 持Scene卡时,牛奶每袋固定售价6.00美元
  • 完整结算内容需包含:商品基础总价、购物袋费用、HST税费、交易总计、Scene积分

修复后完整代码

using System;
using System.Collections.Generic;

class TransactionCalculator
{
    static void Main()
    {
        // 定义商品基础单价
        Dictionary<string, double> itemBasePrices = new Dictionary<string, double>
        {
            {"apples", 1.75},
            {"chillies", 1.29},
            {"tomatoes", 1.45},
            {"milk", 6.54}
        };

        Console.WriteLine("请输入购买的商品数量:");
        if (!int.TryParse(Console.ReadLine(), out int numberOfItems) || numberOfItems <= 0)
        {
            Console.WriteLine("输入无效,请输入正整数。");
            return;
        }

        List<Item> purchasedItems = new List<Item>();

        // 循环录入每件商品信息
        for (int i = 0; i < numberOfItems; i++)
        {
            Console.WriteLine($"\n请输入第{i+1}件商品名称(apples/chillies/tomatoes/milk):");
            string itemName = Console.ReadLine().Trim().ToLower();

            if (!itemBasePrices.ContainsKey(itemName))
            {
                Console.WriteLine("无效的商品名称,请重新输入。");
                i--; // 重新录入当前商品
                continue;
            }

            Console.WriteLine($"请输入{itemName}的购买数量/重量(牛奶为袋数,其他为磅数):");
            if (!double.TryParse(Console.ReadLine(), out double quantity) || quantity <= 0)
            {
                Console.WriteLine("输入无效,请输入正数。");
                i--;
                continue;
            }

            purchasedItems.Add(new Item { Name = itemName, Quantity = quantity, BasePrice = itemBasePrices[itemName] });
        }

        // 输入购物袋数量
        Console.WriteLine("\n请输入使用的购物袋数量:");
        if (!int.TryParse(Console.ReadLine(), out int numberOfBags) || numberOfBags < 0)
        {
            Console.WriteLine("输入无效,请输入非负整数。");
            return;
        }

        // 确认是否持有Scene卡
        Console.WriteLine("是否持有Scene卡?(yes/no):");
        bool hasSceneCard = Console.ReadLine().Trim().ToLower() == "yes";

        // 计算各项费用与积分
        double totalBasePrice = 0;
        int totalScenePoints = 0;

        Console.WriteLine("\n=== 商品明细 ===");
        foreach (var item in purchasedItems)
        {
            double itemPrice;
            if (item.Name == "milk")
            {
                // 牛奶的折扣逻辑
                itemPrice = hasSceneCard ? 6.00 * item.Quantity : item.BasePrice * item.Quantity;
            }
            else
            {
                // 其他商品10%减免逻辑
                double discountedUnitPrice = hasSceneCard ? item.BasePrice * 0.9 : item.BasePrice;
                itemPrice = discountedUnitPrice * item.Quantity;
            }
            totalBasePrice += itemPrice;
            totalScenePoints += (int)(item.Quantity * 20);

            // 格式化输出商品明细
            Console.WriteLine($"{item.Name}:数量 {item.Quantity},单价 ${(hasSceneCard && item.Name != "milk" ? item.BasePrice*0.9 : hasSceneCard ? 6.00 : item.BasePrice):F2},总价 ${itemPrice:F2}");
        }

        double storeBagsCost = numberOfBags * 0.5;
        double subtotal = totalBasePrice + storeBagsCost;
        double hstAmount = subtotal * 0.13;
        double grandTotal = subtotal + hstAmount;

        // 输出结算总计
        Console.WriteLine("\n=== 交易结算 ===");
        Console.WriteLine($"商品基础总价:${totalBasePrice:F2}");
        Console.WriteLine($"购物袋费用:${storeBagsCost:F2}");
        Console.WriteLine($"税前小计:${subtotal:F2}");
        Console.WriteLine($"HST税费(13%):${hstAmount:F2}");
        Console.WriteLine($"交易总计:${grandTotal:F2}");
        Console.WriteLine($"累计Scene积分:{totalScenePoints}");
    }

    // 定义商品类存储信息
    class Item
    {
        public string Name { get; set; }
        public double Quantity { get; set; }
        public double BasePrice { get; set; }
    }
}

关键修改说明

  • 多商品支持:使用列表存储每件商品信息,通过循环完成批量录入,最终汇总计算
  • 修正拼写错误:将所有Scena改为正确的Scene
  • 修复折扣逻辑:非牛奶类商品先计算减免后的单价(原价×0.9)再乘以数量;牛奶类直接使用固定折扣价6美元计算
  • 输入校验:添加TryParse确保输入有效性,避免程序崩溃
  • 明细展示:输出每件商品的单价、总价,让交易更透明
  • 积分汇总:累计所有商品的Scene积分,而非仅单个商品

内容的提问来源于stack exchange,提问作者user15610976

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 02:45:18