2014-09-29 91 views
0

我在写这个程序,并且无法获得两个项目来显示。被征税的变量在每个输出中显示0,并且纳税人编号在输出中根本不显示...有人可以解释为什么会发生这种情况吗?遇到对象数组输出问题

class Rates 
{ 
    private int limit; 
    private double lowRate; 
    private double highRate; 

    public int Limit 
    { 
     get 
     { 
      return limit; 
     } 
    } 
    public double LowRate 
    { 
     get 
     { 
      return lowRate; 
     } 
    } 
    public double HighRate 
    { 
     get 
     { 
      return highRate; 
     } 
    } 

    public void setRates() 
    { 
     limit = 30000; 
     lowRate = 0.15; 
     highRate = 0.28; 
    } 

    public void setRates(int iLimit, double lRate, double hRate) 
    { 
     Console.WriteLine("Enter dollar limit: "); 
     iLimit = Convert.ToInt32(Console.ReadLine()); 
     limit = iLimit; 
     Console.WriteLine("Enter the low rate: "); 
     lRate = Convert.ToDouble(Console.ReadLine()); 
     lowRate = lRate; 
     Console.WriteLine("Enter the high rate: "); 
     hRate = Convert.ToDouble(Console.ReadLine()); 
     highRate = hRate; 
    } 

    public void CalculateTax(int userIncome) 
    { 
     if (userIncome < limit) 
      Console.Write(userIncome * lowRate); 
     else 
      Console.Write(userIncome * highRate); 
    } 
} 
class TaxPayer : IComparable 
{ 
    private String ssn; 
    private double grossIncome; 
    private double taxOwed; 

    public String SSN 
    { 
     get 
     { 
      return this.ssn; 
     } 
     set 
     { 
      this.ssn = value; 
     } 
    } 
    public double GrossIncome 
    { 
     get 
     { 
      return this.grossIncome; 
     } 
     set 
     { 
      this.grossIncome = value; 
     } 
    } 
    public double TaxOwed 
    { 
     get 
     { 
      return taxOwed; 
     } 
    } 

    public void CalcTax() 
    { 
     Rates firstRate = new Rates(); 
     if (GrossIncome < firstRate.Limit) 
      taxOwed = GrossIncome * firstRate.LowRate; 
     else 
      taxOwed = GrossIncome * firstRate.HighRate; 
    } 

    int IComparable.CompareTo(object o) 
    { 
     int returnVal; 
     TaxPayer temp = (TaxPayer)o; 
     if (this.TaxOwed < temp.TaxOwed) 
      returnVal = -1; 
     else 
      returnVal = 0; 
     return returnVal; 
    } 

    public void getRates() 
    { 
     int income = 0; 
     double lowRate = 0; 
     double highRate = 0; 
     int limit = 0; 
     char userInput; 
     char upper; 
     Rates newRates = new Rates(); 


     Console.WriteLine("Do you want default values ('D') or enter your own ('O')? "); 
     userInput = Convert.ToChar(Console.ReadLine()); 
     upper = char.ToUpper(userInput); 

     if (upper == 'D') 
      newRates.setRates(); 
     newRates.CalculateTax(income); 
     if (upper == 'O') 
      newRates.setRates(limit, lowRate, highRate); 
    } 
    static void Main() 
    { 
     TaxPayer[] tpArray = new TaxPayer[5]; 
     int x; 
     for (x = 0; x < tpArray.Length; ++x) 
      tpArray[x] = new TaxPayer(); 
     for (x = 0; x < tpArray.Length; ++x) 
     { 
      Console.WriteLine("Enter the social scurity number: "); 
      tpArray[x].SSN = Convert.ToString(Console.ReadLine()); 
      Console.WriteLine("Enter the gross income for the taxpayer: "); 
      tpArray[x].GrossIncome = Convert.ToDouble(Console.ReadLine()); 
      tpArray[x].getRates(); 
      tpArray[x].CalcTax(); 
     } 
     for (x = 0; x < tpArray.Length; ++x) 
     { 
      Console.WriteLine("Taxpayer # {0} SSN: {1} income {2} and tax owed is {3}", tpArray[x], tpArray[x].SSN, 
       tpArray[x].GrossIncome, tpArray[x].TaxOwed.ToString("C")); 
     } 

     Array.Sort(tpArray); 
     for (x = 0; x < tpArray.Length; ++x) 
     { 
      Console.WriteLine("Taxpayer # {0} SSN: {1} income {2} and tax owed is {3}", tpArray[x], tpArray[x].SSN, 
       tpArray[x].GrossIncome, tpArray[x].TaxOwed.ToString("C")); 
     } 
     Console.ReadLine(); 
    } 
} 

}

回答

0

使用调试器,踏进这行:tpArray[x].CalcTax();,你会看到这个问题:你是不是使用由用户输入的速度,但创造的目的,一个新的Rates对象计算。

当然,这个新的Rates对象没有初始化,并且速率都是零。

的另一个问题是,为了输出纳税人#,你应该输出x,没有你的帮助Zruty tpArray[x]

+0

谢谢,我不得不创建CalcTax方法给我只读TaxOwed的值。是否有另一种方法来填补TaxOwed变量的缺失值? – Joe 2014-09-29 03:39:00

+0

你可以用多种方式解决你的问题。你应该看一个更高层次的画面:你为什么需要'价格'对象?为什么你需要'TaxPayer'对象?然后弄清楚如何正确地将它们连接在一起 – Zruty 2014-09-29 04:07:18