1

不知道我在这里做错了什么。我有一个程序要求输入,2 个选项中的 1 个,然后显示相关选项的成本。

我收到错误消息“在 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
        }
    }
}

4

2 回答 2

1

Bro the reason why errors is Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString(D));is telling the runtime environment to set it to datetime format. change it to Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString());

于 2016-06-18T06:20:05.840 回答
0

问题来自小数点上的 ToString 方法。您可以请求特定格式,请在此处查看它们:

ToString(格式)

于 2016-06-18T06:11:51.457 回答