2014-10-03 79 views
-1

我遇到了一些需要爲c#類編寫的應用程序的問題。我收到一個未處理的異常錯誤。我知道這個問題依賴於program.cs和TaxMath.cs,但我不確定該從哪裏下手,任何指導都將不勝感激。稅務計算控制檯應用程序問題

錯誤:

Unhandled Exception: System.FormatException: Index (zero based) must be greater 
 
than or equal to zero and less than the size of the argument list. 
 
    at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String fo 
 
rmat, Object[] args) 
 
    at System.String.Format(IFormatProvider provider, String format, Object[] arg 
 
s) 
 
    at System.IO.TextWriter.WriteLine(String format, Object arg0) 
 
    at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0) 
 
    at System.Console.WriteLine(String format, Object arg0) 
 
    at TaxProgram.Program.Main(String[] args) in c:\Users\xxxx\Desktop\ConsoleApp 
 
lication2\Program.cs:line 26

class Program 
 
      { 
 

 
       static void Main(string[] args) 
 
       { 
 
        TaxValues tv = new TaxValues(); 
 

 
        Console.WriteLine("Enter in your income: \r\n"); 
 

 
        double income = Convert.ToDouble(Console.ReadLine()); 
 

 
        Console.WriteLine("Enter in single, marryjointly, marryseperate, or  headohouse to enter in your filing status: \r\n"); 
 

 
        FilingStatus fs = (FilingStatus)Enum.Parse(typeof(FilingStatus), Console.ReadLine()); 
 

 
    (line 24)  Tax TaxOwed = new Tax(tv, fs, income); 
 

 
    (line 26)  Console.WriteLine("Your tax is: {1} \r\n ", TaxOwed.calculate());

class Tax 
 
      { 
 

 
       private TaxValues _taxvalues; 
 
       private FilingStatus _filingstatus; 
 
       double _income; 
 
       
 
       public Tax (TaxValues tv, FilingStatus fs, double income) 
 
       { 
 
    (line 18)  Income = income; 
 
        _taxvalues = tv; 
 
        _filingstatus = fs; 
 

 
       } 
 
        public double Income 
 
        { 
 
         get { return _income; } 
 
         set { 
 
          if (value <= 0) 
 
          { 
 
           throw new ArgumentException(String.Format("{0} must be > 0", value)); 
 
          }  
 
          _income = value; 
 
    (line 32)    calculate(); 
 
         } 
 
        } 
 

 
        private void calculate() 
 
        { 
 
         double TaxOwed = 0.0; 
 

 
         if (_filingstatus == FilingStatus.single) 
 
         { 
 
    (line 42)    if (Income <= _taxvalues.Single10) 
 
           TaxOwed = Income * .1; 
 
          else if (Income <= _taxvalues.Single15) 
 
           TaxOwed = Income * .15; 
 
          else if (Income <= _taxvalues.Single25) 
 
           TaxOwed = Income * .25; 
 
          else if (Income <= _taxvalues.Single28) 
 
           TaxOwed = Income * .28; 
 
          else if (Income <= _taxvalues.Single33) 
 
           TaxOwed = Income * .33; 
 
          else if (Income > _taxvalues.Single33) 
 
           TaxOwed = Income * .35; 
 
          else Console.WriteLine("You dun goofed"); 
 
          .. 
 
          return TaxOwed 0.0

+0

學習閱讀例外的細節。它很重要*哪個*例外,以及*哪一行*造成它。然後隔離導致它的代碼,並只發布該代碼。接下來,瞭解數組 - 你的代碼會更短。 – Blorgbeard 2014-10-03 01:57:46

+0

可能的重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-it) – Blorgbeard 2014-10-03 02:06:43

+0

我沒有知識來理解答案的一般性。 – donutvamp 2014-10-03 02:18:45

回答

0

你在你的財產進行計算:

public double Income 
    { 
     get { return _income; } 
     set 
     { 
      if (value <= 0) 
      { 
       throw new ArgumentException(String.Format("{0} must be > 0", value)); 
      } 
      _income = value; 
      //calculate(); <<ISSUE IS HERE 
     } 
    } 

改變你的方法的簽名返回雙:

public double calculate() 
{ 
    double TaxOwed = 0.0; 
    ... 
    return TaxOwed; 
} 

最後登錄到控制檯:

Console.WriteLine("Your tax is: {0} \r\n ", TaxOwed.calculate()); 
+0

它說'不能將null轉換爲double,因爲它是非空值類型' – donutvamp 2014-10-03 02:33:08

+0

@donutvamp對不起,這是一個錯字,你可以初始化'double TaxOwed = 0.0;' – meda 2014-10-03 02:34:42

+0

現在應該沒有錯誤,你需要改變到0這裏'你的稅是:{0}' – meda 2014-10-03 02:40:34