【问题标题】:Receipt like output in c#收据像c#中的输出
【发布时间】:2022-01-05 17:00:21
【问题描述】:
 int choice, quanti, decide, total, cash;
 double change;
 string dcount;
    while (true)
    {
        Console.Clear();
        string w = "WELCOME ";
        Console.SetCursorPosition((Console.WindowWidth - w.Length) / 2, Console.CursorTop);  // for setting string output on center top
        Console.WriteLine(w);
        Console.WriteLine("");
        System.Threading.Thread.Sleep(2000); //time delay

        string p = "HERE'S OUR MERCHANDISES! ";
        Console.SetCursorPosition((Console.WindowWidth - p.Length) / 2, Console.CursorTop);  // for setting string output on center top
        Console.WriteLine(p);
        System.Threading.Thread.Sleep(2000);//time delay

        string[] products = { "[1]BLACKPINK Lightstick ", "[2]DREAMCATCHER Seasons Greetings",
                "[3]RED VELVET Summer Package"};

        for (int g = 0; g < products.Length; g++)
        {
            Console.WriteLine(products[g]);
        }

        Dictionary<int, int> ProductList = new Dictionary<int, int>();
        while (true)
        {
            {
                Console.Write("Pick your product: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("BLACKPINK Lightstick 1500php");
                        break;
                    case 2:
                        Console.WriteLine("DREAMCATCHER Seasons Greetings 920php");

                        break;
                    case 3:
                        Console.WriteLine("RED VELVET Summer Package 980php");

                        break;
                }
                Console.WriteLine("Quantity of product: ");
                quanti = int.Parse(Console.ReadLine());
                if (!ProductList.ContainsKey(choice))
                    ProductList.Add(choice, quanti);
                else
                    ProductList[choice] += quanti;
                System.Threading.Thread.Sleep(2000);//time delay
                Console.WriteLine("[1] Add more products \t [2] Pay: ");
                decide = int.Parse(Console.ReadLine());

                if (decide == 2)
                    break;
            }
        }
        total = 0;
        foreach (int key in ProductList.Keys)
            switch (key)
            {
                case 1:
                    total += ProductList[key] * 1500;
                    break;
                case 2:
                    total += ProductList[key] * 920;
                    break;
                case 3:
                    total += ProductList[key] * 980;
                    break;
                default:
                    total += 0;
                    break;
            };
        Console.WriteLine("To Pay: " + total);
        Console.Write("Cash: ");
        cash = Convert.ToInt32(Console.ReadLine());
        Console.Write("Discount[s]suki [v]voucher: ");
        dcount = Console.ReadLine();
        if (dcount == "s") 
        {
           change = (cash - total) - 0.7;
            Console.WriteLine("Change: " + change);

        }
        else if (dcount == "v")
        {
           change = cash -(total - 0.5) ;
            Console.WriteLine("Change: " + change);

        }
        else 
        {
            change = cash- (total - 0.7) ;
            Console.WriteLine("Change: " + change);
        }

     

        Console.WriteLine("Another shopping? [1]Yes [2] No");
        choice = int.Parse(Console.ReadLine());
        if (choice == 2)
            break;


  }
}

} } 美好的一天,我正在为学校做一个项目。我之前已经询问过我的代码,有人帮助我,感谢他,不幸的是我的代码仍然有问题。在用户输入并给她找零之后,它应该输出一张收据,其中包含用户购买的产品。我曾尝试输出用于获取用户输入的变量,但令我沮丧的是,它只能输出用户的第一个输入产品,它不能输出所有用户购买的产品。类似输出的收据应该包含用户购买的产品、每种产品的数量、总计、现金、折扣和横线/表格的变化。

【问题讨论】:

    标签: c# loops receipt


    【解决方案1】:

    打印出每个产品和数量的快速方法是在dcount = Console.ReadLine(); 之后放置类似:

    foreach(var entry in ProductList)
    {
        Console.WriteLine(products[entry.Key].Substring(3) + "......." + entry.Value);
    }
    

    我会把点的数量和光标位置留给你。

    另外,在处理货币时最好使用decimal,因为它的精度更高(在本例中可能不需要。)并在显示金额时尝试使用Math.Round() 函数 - 限制小数位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-22
      • 2019-08-04
      • 2010-11-24
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多