2013-04-10 107 views
1

一切似乎運行良好,除了顯示的稅款始終爲0.我無法弄清楚如何正確獲取默認或用戶條目的計算。這裏是我有任何幫助的代碼將不勝感激。無法正確顯示納稅結果

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace TestApp 
{ 
class Rates 
{ 
    public readonly int incomeLimit; 
    public readonly double lowTax; 
    public readonly double highTax; 

    public Rates() 
    { 
     incomeLimit = 30000; 
     lowTax = .15; 
     highTax = .28; 
    } 
    public Rates(int limit, double lowRate, double highRate) 
    { 
     limit = incomeLimit; 
     lowRate = lowTax; 
     highRate = highTax; 
    } 
    public double CalcTax(double income) 
    {    
     double tax; 
     if (income < incomeLimit) 
     tax = income * lowTax; 
     else 
      tax = income * highTax; 

     return tax; 
    } 
} 
class Taxpayer : IComparable <Taxpayer> 
{ 
    public string social; 
    public double grossincome; 
    public double taxowed; 

    public string SSN 
    { 
     get 
    { 
     return social; 
    } 
     set 
    { 
     social = value; 
    } 
    } 
    public double grossIncome 
    { 
     get 
     { 
      return grossincome; 
     } 
     set 
     { 
      grossincome = value; 
     } 
    } 
    public double taxOwed 
    { 
     get 
     { 
      return taxowed; 
     } 
    } 

    public int CompareTo(Taxpayer o) 
    {   
     return this.taxOwed.CompareTo(o.taxOwed); 
    } 
    public static void getRates() 
    { 
     Rates rates = new Rates(); 
     Taxpayer tax = new Taxpayer(); 

     int limit = 0; 
     double lowRate = 0; 
     double highRate = 0; 
     char input; 
     Console 
      .Write("Do you want default values (enter D) or enter your own (enter O)? "); 
     input = Char.ToUpper(Convert.ToChar(Console.ReadLine())); 

     switch (input) 
     { 
      case 'D': 
       Rates def = new Rates(); 
       limit = def.incomeLimit; 
       lowRate = def.lowTax; 
       highRate = def.highTax; 
       tax.taxowed = def.CalcTax(tax.grossIncome); 
       break; 
      case 'O': 
       Rates own = new Rates(limit, lowRate, highRate); 
       Console.Write("Enter the dollar limit "); 
       limit = Convert.ToInt32(Console.ReadLine()); 
       Console.Write("Enter the low rate "); 
       lowRate = Convert.ToDouble(Console.ReadLine()); 
       Console.Write("Enter the high rate "); 
       highRate = Convert.ToDouble(Console.ReadLine()); 
       tax.taxowed = own.CalcTax(tax.grossIncome); 
       break; 
     } 

    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Taxpayer[] taxarray = new Taxpayer[5]; 

     for (int x = 0; x < taxarray.Length; ++x) 
     { 
      taxarray[x] = new Taxpayer(); 
      Console.Write("Enter Social Security Number for taxpayer {0}: ", x+1); 
      taxarray[x].SSN = Console.ReadLine().Replace("-", ""); 

      Console.Write("Enter gross income for taxpayer {0}: ", x+1); 
      taxarray[x].grossIncome = Convert.ToDouble(Console.ReadLine()); 
      //Taxpayer.getRates(); 
      Taxpayer.getRates(); 
     } 

     Console.WriteLine(); 
     for (int i = 0; i < taxarray.Length; i++) 
     {    
      Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", 
       i+1,taxarray[i].SSN, taxarray[i].grossIncome, taxarray[i].taxOwed); 
     } 

     Console.WriteLine("-------------------------------------------------------"); 
     Array.Sort(taxarray); 
     for (int i = 0; i < taxarray.Length; i++) 
     { 
      Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", 
       i+1,taxarray[i].SSN, taxarray[i].grossIncome, taxarray[i].taxOwed); 
     }    
    } 
} 
} 
+0

你調試了你的代碼嗎? VS有一個內置的。 – I4V 2013-04-10 20:28:39

+0

@ I4V:他可能是一名初學者程序員。沒什麼錯,大聲笑​​... – code4life 2013-04-10 22:08:54

+0

@ code4life所以你說你喜歡閱讀文本牆。然後請發表回答,而不是回覆評論。 *大聲笑.... * – I4V 2013-04-10 22:34:29

回答

1

主要問題似乎是您正在調用靜態方法(​​)。您輸入的值將存儲在該方法中新創建的TaxPayer實例中,但它們永遠不會複製到您在數組中創建的實例。

我想你想的是一個實例方法,在你輸入迴路稱呼它:

taxarray[x].getRates(); 

這將需要一些更改​​方法,尤其是具有它使用this引用,而比創建一個新的TaxPayer實例。

+0

謝謝。工作很好。 – Evan 2013-04-11 01:55:12