从双精度到字符串的格式不正确 Book Example C#
incorrect format from double to string Book Example C#
所以我正在按照我书中的一个示例进行操作,该示例为我提供了输入 visual studio 的代码。我完全按照教科书上的代码键入了代码,但我无法将程序设置为 运行。
Console.Write("{ 0, 8}", tipRate.ToString("F"));
抛出
Input string was not in a correct format
我不知道是什么问题。也许自本书出版(2012 年)以来 Visual Studio 发生了一些变化?对此的任何帮助将不胜感激,因为我只是不明白我做错了什么。
class Program
{
static void Main(string[] args)
{
double dinnerPrice = 10.00;
double tipRate;
double tip;
const double LOWRATE = 0.10;
const double MAXRATE = 0.25;
const double TIPSTEP = 0.05;
const double MAXDINNER = 100.00;
const double DINNERSTEP = 10.00;
Console.Write(" Price");
for (tipRate = LOWRATE; tipRate <= MAXRATE; tipRate += TIPSTEP)
Console.Write("{ 0, 8}", tipRate.ToString("F"));
Console.WriteLine();
Console.WriteLine
("——————————————————————————————————————");
tipRate = LOWRATE;
while (dinnerPrice <= MAXDINNER)
{
Console.Write("{ 0, 8}", dinnerPrice.ToString("C"));
while (tipRate <= MAXRATE)
{
tip = dinnerPrice * tipRate;
Console.Write("{ 0, 8}",tip.ToString("F"));
tipRate += 0.05;
}
dinnerPrice += DINNERSTEP;
tipRate = LOWRATE;
Console.WriteLine();
}
}
}
您的字符串有错字。删除 {
后的空格
Console.Write("{0, 8}", tipRate.ToString("F"));
请注意,您在多个地方遇到了同样的问题,您需要在所有地方进行修复。
所以我正在按照我书中的一个示例进行操作,该示例为我提供了输入 visual studio 的代码。我完全按照教科书上的代码键入了代码,但我无法将程序设置为 运行。
Console.Write("{ 0, 8}", tipRate.ToString("F"));
抛出
Input string was not in a correct format
我不知道是什么问题。也许自本书出版(2012 年)以来 Visual Studio 发生了一些变化?对此的任何帮助将不胜感激,因为我只是不明白我做错了什么。
class Program
{
static void Main(string[] args)
{
double dinnerPrice = 10.00;
double tipRate;
double tip;
const double LOWRATE = 0.10;
const double MAXRATE = 0.25;
const double TIPSTEP = 0.05;
const double MAXDINNER = 100.00;
const double DINNERSTEP = 10.00;
Console.Write(" Price");
for (tipRate = LOWRATE; tipRate <= MAXRATE; tipRate += TIPSTEP)
Console.Write("{ 0, 8}", tipRate.ToString("F"));
Console.WriteLine();
Console.WriteLine
("——————————————————————————————————————");
tipRate = LOWRATE;
while (dinnerPrice <= MAXDINNER)
{
Console.Write("{ 0, 8}", dinnerPrice.ToString("C"));
while (tipRate <= MAXRATE)
{
tip = dinnerPrice * tipRate;
Console.Write("{ 0, 8}",tip.ToString("F"));
tipRate += 0.05;
}
dinnerPrice += DINNERSTEP;
tipRate = LOWRATE;
Console.WriteLine();
}
}
}
您的字符串有错字。删除 {
Console.Write("{0, 8}", tipRate.ToString("F"));
请注意,您在多个地方遇到了同样的问题,您需要在所有地方进行修复。