参数错误c#

tin*_*tes 1 c#

我正在做一本书中的例子:Herbert Schildt撰写的完整参考文献C#3.0.它是关于使用参数在Console.WriteLine中编写的.这是一个例子:我试过这个但是我收到了一个错误:

Project1.exe has encountered a problem and needs to be close. We are sorry for the inconvenience. Please tell Microsoft about this problem. Send Error Report or Don't Send. And if I click, I get another error in the command prompt. "Unhandled Exception: System.Format.Exception input string was not in a correct format. 
at System.Text.StringBuilder.AppendFormatError()
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider,String Format, Object[]args)
at  System.IO.TextWriter.WriteLine(String format, Object arg0)
at  System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
At Example2.Main() in D:\myPath

我不确定这本书是否有错误或是我的代码?我很感激你的帮助.谢谢

指定格式的最简单方法之一是描述WriteLine()将使用的模板.为此,请显示所需格式的示例,使用#s标记数字位置.您还可以指定小数点和逗号.例如,这是一个更好的方式来显示10除以3:
Console.WriteLine("这里是10/3:{0:#.##}",10.0/3.0);
此语句的输出如下所示:这是10/3:3.33

顺便说一句,这是我的代码看起来像:

   static void Main()
    { 
   Console.WriteLine("Here is 10/3: {0:#.##)", 10);
    }
Run Code Online (Sandbox Code Playgroud)

ang*_*son 9

您使用错误的结束括号格式参数.

注意#.##之后的结尾括号),它应该是}}(花括号).

另请注意,您已经省略了分区,如果您只是将代码更改为此(也更正了大括号):

static void Main()
{ 
    Console.WriteLine("Here is 10/3: {0:#.##}", 10/3);
}
Run Code Online (Sandbox Code Playgroud)

那么你也会有另一个问题,因为结果会是:

Here is 10/3: 3.00
Run Code Online (Sandbox Code Playgroud)

原因是10/3是整数除法,看看3在3中完全上升了3次,这是3次.

如果你想要浮点除法,将10除以3得到3和1/3,那么你需要确保至少有一个数是浮点数,因此10.0/3会做.