不确定我在这里做错了什么。我有一个程序,要求输入,2个选项之一,然后显示相关选项的成本。
我收到错误消息“mscorlib.dll中发生了'System.FormatException'类型的未处理异常
附加信息:字符串必须只有一个字符长。“
我的代码出了什么问题?见下文:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace computerPackage
{
class Program
{
static void Main(string[] args)
{
char computerPackage;
const decimal DELUXE_PACKAGE = 1500;
const decimal SUPER_PACKAGE = 1700;
Console.Write("Input the Computer Package D or S: ");
computerPackage = char.Parse(Console.ReadLine());
computerPackage = Char.ToUpper(computerPackage);
if (computerPackage == 'D')
{
Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString("D"));
}
else if (computerPackage == 'S')
{
Console.WriteLine("Cost of Deluxe Computer Package is " +
SUPER_PACKAGE.ToString("S"));
}
else
{
Console.WriteLine("Package D or S not entered");
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(); // pause
}
}
}
答案 0 :(得分:1)
说明错误Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString(D));
告诉运行时环境将其设置为datetime格式的原因。将其更改为Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString());
答案 1 :(得分:0)
问题来自小数点上的ToString方法。您可以请求特定格式,请在此处查看: