2014-09-29 96 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