Can't get anything but $0 from a call to a method to calculate a taxAmount

前端 未结 5 499
-上瘾入骨i
-上瘾入骨i 2021-01-22 11:00

I am having trouble with this following call, specially the last component:

Console.WriteLine(\"Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}\", i + 1,          


        
5条回答
  •  生来不讨喜
    2021-01-22 11:47

    Your problem is in your usage of the Rates.assignRates() method. You are only calling it from the static Taxpayer.GetRates() method. This method is acting on a local Rates object, and then then throwing the populated object away. You probably want to change Taxpayer.GetRates() to return a Rates object, returning the internally created (and populated) rates variable:

    public static Rates GetRates() { ... return rates; }
    

    And then in Main(), remove the existing call to Taxpayer.GetRates() and change the line where you declare the taxRates variable as follows:

    Rates taxRates = Taxpayer.GetRates();
    

    Also note that you should also be handling error cases due to bad/missing input somehow, but you don't seem to be doing that right now, so I didn't include any functional changes other than to get you back the populated Rates object.

    Additionally, you might want to consider making the Rates class static, as you appear to only be using a single instance of it throughout.

提交回复
热议问题