繁体   English   中英

我可以从一个 C# 控制台复制到另一个吗?

[英]Can I copy from one C# console to another?

我最初使用 C# Shell Offline Compiler 来做我的项目,学校最近为我们所有人提供了一份 Visual Studio 2010 Ultimate。 我已经复制了几个控制台程序,它们运行得很好,除了最新的它在一个特定的行中不断给我一个错误。 编译器一直显示的是STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name.PadRight(20)} Price: {string.Format("{0:C2}", x.Price)}")); 有一些无效的论点。 请注意,我已将程序复制到 Visual C# 的控制台部分


    public static class Program 
    {
        public static void Main() 
        {



           Console.WriteLine("                    *****--------------------*****");
           Console.WriteLine("                          Wit Groceries 2020");
           Console.WriteLine("                    *****--------------------*****");
           Console.WriteLine("            Welcome, would you like to do some shopping today?");
           Console.WriteLine("");

           var STOCK = new List<Items>
           {
            new Items{ id = 1, Name = "Bun", Price = 100},
            new Items{ id = 2, Name = "Soda", Price = 80},
            new Items{ id = 3, Name = "Cheese", Price =70},
            new Items{ id = 4, Name = "Tissue", Price = 50},
            new Items{ id = 5, Name = "Fabuloso", Price = 140},
            new Items{ id = 6, Name = "Grace Mackerel", Price = 90},
            new Items{ id = 7, Name = "Rice", Price = 50},
            new Items{ id = 8, Name = "Flour", Price = 40},
            new Items{ id = 9, Name = "Sugar", Price = 30},
           };



           var STOCKDict = STOCK.ToDictionary(i=>i.id);

           var selecteditems = new List<Items>();

           STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name.PadRight(20)} Price: {string.Format("{0:C2}", x.Price)}"));
           Console.WriteLine();

           Console.WriteLine(" Enter the number for the item you want, enter 'x' when finihshed");


           do
           {
            var choice = Console.ReadLine();
            if (choice.Trim() =="x")
            {
                break;
            }
            int id;
            if (!int.TryParse(choice, out id))
            {
               Console.WriteLine("Enter number or 'x' ");
               continue;
            }
            if (STOCKDict.ContainsKey(id))
            {
                var item = STOCKDict[id];
                selecteditems.Add(item);
                Console.WriteLine(item.Name + " is added to your list");
            }
            else
            {
                Console.WriteLine(id + " is not available");
            }
           }
           while (true);

           decimal subt = selecteditems.Sum(i=>i.Price);
           decimal GCT = 16.50m;
           decimal GCTamt = (subt*GCT/100);
           decimal total = subt+GCTamt;
           Console.WriteLine("                  Sub-total = " + "$" + subt);
           Console.WriteLine("                  GCT = " + "$" + GCTamt);
           Console.WriteLine("                  You pay " + "$" +total);
           Console.WriteLine();

           while (true)
        {
            Console.WriteLine(                  "Please tender cash");
            decimal tend = decimal.Parse(Console.ReadLine());

            if (tend> total)
            {
                decimal change = tend - total;
                Console.WriteLine("You gave " + "$" + tend + " Your change is " + "$" + change);

                break;
            }

            if (tend==total)
            {
                Console.WriteLine(           "No Change, exact amount given");
                break;
            }
            if (tend<total)
            {
                Console.WriteLine(          "Incorrect amount, more cash needed");
            }
        }



        }

    }
    public class Items
    {
        public int id {get; set;}
        public string Name{get; set;}
        public decimal Price {get;set;}
    }
}

您不需要在插值字符串的中间执行 string.Format ,因为插值已经在进行格式化:

STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name.PadRight(20)} Price: {string.Format("{0:C2}", x.Price)}"));

会更简单:

STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name,-20} Price: {x.Price:C2}"));

——

然而,我认为,问题在于 VS2010 太老了,无法理解内插字符串语法; interp 出现在 c# 6 中,可与 VS2013+ afaik 一起使用。 你的学校并没有真正帮你什么忙给你一个十年前的 IDE,尤其是看到 VS2019 有一个你可以使用的免费版本

您必须删除内插字符串并使用旧样式格式:

STOCK.ForEach(x => Console.WriteLine(string.Format("ID {0} - Name: {1,-20} Price: {2:C2}", x.id, x.Name, x.Price)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM