未处理的出现FormatException错误在C#

时间:2013-11-30 16:34:04

标签: c# override formatexception mscorlib

所以我的上一篇文章因为过于宽泛而被关闭了,所以我修了一些东西并缩小了范围。现在当我尝试运行它时,我在第136行得到错误,它是CheckingAccount类中的PrintAccount覆盖和第148行,它是AccountTest中的Main类。弹出的错误说:“mscorlib.dll中发生了'System.FormatException'类型的未处理异常附加信息:输入字符串的格式不正确。”我不知道如何解决它。任何指导将不胜感激。感谢。

namespace Assignment5
{
    class Account
    {
        //variables
        private double balance;
        private string accountName;
        private int accountNumber;

        public Account(string acctName, int acctNum, double acctBal)
        {
            accountName = acctName;
            accountNumber = acctNum;
            balance = acctBal;
        }
        //gets and sets
        public double Balance
        {
            get { return balance; }
            set
            {
                if (value >= 0)
                    balance = value;
                else
                    balance = 0;
            }
        }
        public string AccountName
        {
            get { return accountName; }
        }
        public int AccountNumber
        {
            get { return accountNumber; }
        }

        //credit, debit and print methods
        public void Credit(double a)
        {
            balance += a;
        }
        public void Debit(double a)
        {
            if (a > balance)
                Console.WriteLine("Insufficient Funds.");
            else
                balance -= a;
        }
        public virtual void PrintAccount()
        {
            Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}",
                accountName, accountNumber, balance);
        }

        class SavingsAccount : Account //this is how the derived class inherits from the base class, right?
        {

            public SavingsAccount(string acctName, int acctNum, double acctBal, double interest)
                : base(acctName, acctNum, acctBal)
            {
                accountName = acctName;
                accountNumber = acctNum;
                balance = acctBal;
                interestRate = interest;
            }
            private double interestRate;
            public double InterestRate
            {
                get { return interestRate; }
                set
                {
                    if (value < 0)
                        interestRate = 0;
                    else
                        interestRate = value;
                }
            }
            public void CalculateInterest()
            {
                balance = (balance * interestRate) + balance;
                Console.WriteLine("Account Name:\t{0}\nAccount Number:\t{1}\nBalance:\t{2:C}\nInterest Rate:\t{3:P2}",
                    accountName, accountNumber, balance, interestRate);
            }
            public override void PrintAccount()
            {
                Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nInterest Rate :\t{3:P2}",
                    accountName, accountNumber, balance, interestRate);
            }
        }
        class CheckingAccount : Account
        {
            public CheckingAccount(string acctName, int acctNum, double acctBal, double fee)
                : base(acctName, acctNum, acctBal)
            {
                accountName = acctName;
                accountNumber = acctNum;
                balance = acctBal;
                feeCharged = fee;
            }
            private double feeCharged;
            public double FeeAmmount
            {
                set
                {
                    if (value < 0)
                        feeCharged = 0;
                    else
                        feeCharged = value;
                }
            }
            //No idea how to "invoke the Account class and use a boolean value to see if
            //money was withdrawn."

            //public void Credit(double a)
            //{
            //    balance += a;
            //    balance -= feeCharged;
            //}
            //public void Debit(double a)
            //{
            //    if (a > balance)
            //        Console.WriteLine("Insufficient Funds.");
            //    else
            //        balance -= a;
            //        balance -= feeCharged;
            //} 

            public override void PrintAccount()
            {
                Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nFee Charged :\t{3:C}",
                    accountName, accountNumber, balance, feeCharged);
            }
        }
        class AccountTest
        {
            static void Main(string[] args)
            {
                //Step 1 & 2
                    CheckingAccount lemmonsChecking = new CheckingAccount("Lemmons-Checking", 1, 1000, 3);
                    SavingsAccount lemmonsSavings = new SavingsAccount("Lemmons-Savings", 2, 2000, .05);
                //Step 3 & 4
                    lemmonsChecking.PrintAccount();
                    lemmonsSavings.PrintAccount();
                //Step 5 & 6
                Console.WriteLine("\nDeposit $100 into checking.");
                    lemmonsChecking.Credit(100);
                    lemmonsChecking.PrintAccount();
                //Step 7 & 8
                    Console.WriteLine("\nWithdraw $50 from checking.");
                    lemmonsChecking.Debit(50);
                    lemmonsChecking.PrintAccount();
                //Step 9 & 10
                    Console.WriteLine("\nWithdraw $6000 from checking.");
                    lemmonsChecking.Debit(6000);
                    lemmonsChecking.PrintAccount();
                //Step 11 & 12
                    Console.WriteLine("\nDeposit $3000 into savings.");
                    lemmonsSavings.Credit(3000);
                    lemmonsSavings.PrintAccount();
                //Step 13 & 14
                    Console.WriteLine("\nWithdraw $200 from savings.");
                    lemmonsSavings.Debit(200);
                    lemmonsSavings.PrintAccount();
                //Step 15 & 16
                    Console.WriteLine("\nCalculate interest on savings.");
                    lemmonsSavings.CalculateInterest();
                //Step 17 & 18
                    Console.WriteLine("\nWithdraw $10,000 from savings.");
                    lemmonsSavings.Debit(10000);
                    lemmonsSavings.PrintAccount();
                    Console.WriteLine("\nPress the [ENTER] key to continue.");
                    Console.ReadLine();
                //I keep receiving errors based around the override I placed on the PrintAccount()


            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您有\t{1)而不是\t{1}

public override void PrintAccount()
{
    Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nFee Charged :\t{3:C}", accountName, accountNumber, balance, feeCharged);
}

应该是

public override void PrintAccount()
{
    Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1}\nBalance :\t{2:C}\nFee Charged :\t{3:C}", accountName, accountNumber, balance, feeCharged);
}