输入对象数组时遇到问题

时间:2014-09-29 03:09:34

标签: c#

我正在编写此程序,无法显示两个项目。在每个输出中,纳税变量显示为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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用调试器,进入以下行:tpArray[x].CalcTax();,您将看到问题:您没有使用用户输入的费率,而是为了计算而创建新的Rates对象。

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

另一个问题是,为了输出纳税人#,你应该输出x,而不是tpArray[x]

相关问题