String.Format()上的方法名称预期错误

时间:2014-07-20 13:22:30

标签: c#

我最近开始学习没有编程经验的c#,我创建了这个超级简单的脚本,用于计算窗口安装的材料成本,但是在String.Format(“{0:0。 ##}“Visual Studio给了我一个错误 - '预期的方法名'。任何帮助都会很棒,因为我无法在任何地方找到足够的解决方案。注意它全部包含在类和Main()方法中如果这可能以某种方式影响它。

    Console.Write("How wide is the window in metres: ");
    decimal frameWidth = Convert.ToDecimal(Console.ReadLine());


    Console.Write("How tall is the window in metres? ");
    decimal frameHeight = Convert.ToDecimal(Console.ReadLine());

    decimal glassArea = (frameWidth *  frameHeight) ;
    decimal woodLength = (frameWidth * 2) + (frameHeight * 2);

    Console.WriteLine("You will need " + glassArea + "m squared of glass and " + woodLength + "m of wood.");

    Console.Write("What is the current cost of glass per square metre? £");
    decimal gCost = Convert.ToDecimal(Console.ReadLine());

    Console.Write("What is the current cost of wood per metre? £");
    decimal wCost = Convert.ToDecimal(Console.ReadLine());

    decimal totalCost = String.Format("{0:0.##}"((glassArea * gCost) + (woodLength * wCost)));
    Console.WriteLine("The total cost for the required materials is £" + totalCost );
    Console.WriteLine("Press any key to exit...");
    Console.ReadLine();

2 个答案:

答案 0 :(得分:3)

参数需要以逗号分隔,因此您必须更改

decimal totalCost = String.Format("{0:0.##}"((glassArea * gCost) + (woodLength * wCost)));

进入

decimal totalCost = String.Format("{0:0.##}", ((glassArea * gCost) + (woodLength * wCost)));

我没有检查其他错误。

答案 1 :(得分:2)

缺少逗号

String.Format("{0:0.##}" ,//<--
             ((glassArea * gCost) + (woodLength * wCost)));