【问题标题】:Billing simulation计费模拟
【发布时间】:2021-07-03 21:21:53
【问题描述】:

我需要创建一个程序,询问客户有多少物品要交给 Bill,然后询问产品名称、数量和价格。完成后,它应该计算所有产品的总价格并打印为.txt 文件。

由于我不熟悉使用数组,所以我被困在第二步...这是我到目前为止的代码(我使用 Visual Studio 2019):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Final_lab
{
    class Program
    {
        static void Main(string[] args) => Factura();

            static void Factura()

            {
            int x;
            Console.WriteLine("Por favor indique la cantidad de registros a ingresar...");
            int cantidad = recibir();
            Console.WriteLine("Por favor indique el nombre del productos, la cantidad y el precio por `favor" +`
                "entre comas ',' ");
            
            Console.ReadLine();
            for (x = 0; x < cantidad; x++) 
            {
                Console.WriteLine("Por favor indique el nombre del productos, la cantidad y el precio por favor" +
                "entre comas ',' ");
                x = recibir();
                int[,] productos;
                productos = new int[, ];

            }
            Console.ReadLine();
        }
            

            private static int recibir()
            {
            int guardar = Convert.ToInt32(Console.ReadLine());
            return guardar;
            }

【问题讨论】:

    标签: c# billing


    【解决方案1】:

    你走错路了,一般来说,你最好使用列表和数据类型,比如:-

            public record BillingLine(string Product, int Quantity, decimal Price); 
            static void Main(string[] args)
            {
                var billingLines = new List<BillingLine>();
                Console.WriteLine("How many?");
                var count = GetInt();
                foreach (var index in Enumerable.Range(0, count))
                {
                    Console.WriteLine("Product Name?");
                    var name = GetString();
                    Console.WriteLine("Quantity");
                    var quantity = GetInt();
                    Console.WriteLine("Price");
                    var price = GetDecimal();
                    billingLines.Add(new BillingLine(name, quantity,price));
                }
    
                Console.WriteLine($"Total to Bill is {billingLines.Select(l => l.Price*l.Quantity).Sum()}");
            }
    
            public static int GetInt() => Convert.ToInt32(Console.ReadLine());
            public static string GetString() => Console.ReadLine().Trim();
            public static decimal GetDecimal() => Convert.ToDecimal(Console.ReadLine());
    

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 2012-06-17
      • 2010-11-03
      • 2014-11-23
      • 1970-01-01
      • 2018-12-27
      • 2016-03-04
      • 2012-08-29
      • 2020-01-08
      相关资源
      最近更新 更多