2012-04-12 45 views
1

問題的方法調用是這樣的WriteLine調用內末有一個方法調用的麻煩,並得到一個正確的值回

Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); 

這裏是利率類的方法是在

public class Rates 
{ 
    // Create a class named rates that has the following data members: 
    int  incLimit; 
    double lowTaxRate; 
    double highTaxRate; 

    // use read-only accessor 
    public int IncomeLimit 
    { get { return incLimit; } } 
    public double LowTaxRate 
    { get { return lowTaxRate; } } 
    public double HighTaxRate 
    { get { return highTaxRate; } } 

    //A class constructor that assigns default values 
    public void assignRates() 
    { 
     incLimit = 30000; 
     lowTaxRate = .15; 
     highTaxRate = .28; 
    } 
    //A class constructor that takes three parameters to assign input values for limit, low rate and high rate. 
    public void assignRates(int lim, double low, double high) 
    { 
     incLimit = lim; 
     lowTaxRate = low; 
     highTaxRate = high; 
    } 
    // A CalculateTax method that takes an income parameter and computes the tax as follows: 
    public int CalculateTax(int income) 
    { 

     int taxOwed; 
     // If income is less than the limit then return the tax as income times low rate. 
     if (income < incLimit) 
      taxOwed = Convert.ToInt32(income * lowTaxRate); 
     // If income is greater than or equal to the limit then return the tax as income times high rate. 
     else 
      taxOwed = Convert.ToInt32(income * highTaxRate); 

     return taxOwed; 
    } 

現在如果我在初始化時在下面的變量中保留一個值,我可以得到一個返回的值,但如果我將它們作爲默認值,我總是從writeline獲得0。

int  incLimit; 
double lowTaxRate; 
double highTaxRate; 

以防萬一你需要看別的東西,剩下的就是這裏。

我也有一個關於排序的問題,我不認爲它工作正常,因爲我需要它按taxOwed的數量進行排序,但目前是0和我的問題。

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

namespace Assignment5_2 
{ 

public class Rates 
{ 
    // Create a class named rates that has the following data members: 
    int  incLimit; 
    double lowTaxRate; 
    double highTaxRate; 

    // use read-only accessor 
    public int IncomeLimit 
    { get { return incLimit; } } 
    public double LowTaxRate 
    { get { return lowTaxRate; } } 
    public double HighTaxRate 
    { get { return highTaxRate; } } 

    //A class constructor that assigns default values 
    public void assignRates() 
    { 
     incLimit = 30000; 
     lowTaxRate = .15; 
     highTaxRate = .28; 
    } 
    //A class constructor that takes three parameters to assign input values for limit, low rate and high rate. 
    public void assignRates(int lim, double low, double high) 
    { 
     incLimit = lim; 
     lowTaxRate = low; 
     highTaxRate = high; 
    } 
    // A CalculateTax method that takes an income parameter and computes the tax as follows: 
    public int CalculateTax(int income) 
    { 

     int taxOwed; 
     // If income is less than the limit then return the tax as income times low rate. 
     if (income < incLimit) 
      taxOwed = Convert.ToInt32(income * lowTaxRate); 
     // If income is greater than or equal to the limit then return the tax as income times high rate. 
     else 
      taxOwed = Convert.ToInt32(income * highTaxRate); 

     return taxOwed; 
    } 


} //end class Rates 

// Create a class named Taxpayer that has the following data members: 
public class Taxpayer : IComparable 
{ 
    //Use get and set accessors. 
    string SSN 
    { set; get; } 
    int grossIncome 
    { set; get; } 
    int taxOwed 
    { set; get; } 

    int IComparable.CompareTo(Object o) 
    { 
     int returnVal; 
     Taxpayer temp = (Taxpayer)o; 
     if (this.taxOwed > temp.taxOwed) 
      returnVal = 1; 
     else if (this.taxOwed < temp.taxOwed) 
      returnVal = -1; 
     else returnVal = 0; 

     return returnVal; 

    } // End IComparable.CompareTo 

    public static void GetRates() 
    { 
     // Local method data members for income limit, low rate and high rate. 

     int incLimit; 
     double lowRate; 
     double highRate; 
     string userInput; 
     Rates rates = new Rates(); 
     // Prompt the user to enter a selection for either default settings or user input of settings. 
     Console.Write("Would you like the default values (D) or would you like to enter the values (E)?: "); 
     /* If the user selects default the default values you will instantiate a rates object using the default constructor 
     * and set the Taxpayer class data member for tax equal to the value returned from calling the rates object CalculateTax method.*/ 
     userInput = (Console.ReadLine()); 
     if (userInput == "D" || userInput == "d") 
     { 

      rates.assignRates(); 
     } // end if 
     /* If the user selects to enter the rates data then prompt the user to enter values for income limit, low rate and high rate, 
     * instantiate a rates object using the three-argument constructor passing those three entries as the constructor arguments and 
     * set the Taxpayer class data member for tax equal to the valuereturned from calling the rates object CalculateTax method. */ 
     else if (userInput == "E" || userInput == "e") 
     { 
      Console.Write("Please enter the income limit: "); 
      incLimit = Convert.ToInt32(Console.ReadLine()); 
      Console.Write("Please enter the low rate: "); 
      lowRate = Convert.ToDouble(Console.ReadLine()); 
      Console.Write("Please enter the high rate: "); 
      highRate = Convert.ToDouble(Console.ReadLine()); 
      //Rates rates = new Rates(); 
      rates.assignRates(incLimit, lowRate, highRate); 
     } 
     else Console.WriteLine("You made an incorrect choice"); 
    } 

    static void Main(string[] args) 
    { 

     Taxpayer[] taxArray = new Taxpayer[5]; 
     Rates taxRates = new Rates(); 
     // Implement a for-loop that will prompt the user to enter the Social Security Number and gross income. 
     for (int x = 0; x < taxArray.Length; ++x) 
     { 
      taxArray[x] = new Taxpayer(); 
      Console.Write("Please enter the Social Security Number for taxpayer {0}: ", x + 1); 
      taxArray[x].SSN = Console.ReadLine(); 

      Console.Write("Please enter the gross income for taxpayer {0}: ", x + 1); 
      taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine()); 
      taxArray[x].taxOwed = taxRates.CalculateTax(taxArray[x].grossIncome); 

     } 


     Taxpayer.GetRates(); 

     // Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax. 
     for (int i = 0; i < taxArray.Length; ++i) 
     { 
      Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxArray[i]);//taxRates.CalculateTax(taxArray[i].grossIncome)); 

     } // end for 
     // Implement a for-loop that will sort the five objects in order by the amount of tax owed 
     Array.Sort(taxArray); 
     Console.WriteLine("Sorted by tax owed"); 
     for (int i = 0; i < taxArray.Length; ++i) 
     { 
      Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); 

     } 
    } //end main 

} // end Taxpayer class 

} //end 
+0

我覺得你問的這個問題http://stackoverflow.com/questions/10116706/cant-get-anything-but-0-from -a-call-to-a-method-to-calculate-a-taxamount/10116729#10116729 – 2012-04-12 05:26:55

回答

0

問題是您沒有調用設置默認速率的代碼。此代碼:

//A class constructor that assigns default values 
public void assignRates() 
{ 
    incLimit = 30000; 
    lowTaxRate = .15; 
    highTaxRate = .28; 
} 

雖然被評論爲「構造函數」不是一個。它永遠不會被調用或運行。由於類被稱爲價格,你需要一個默認的構造函數叫做價格:

public Rates() 
{ 
    // assign the default rates in your constructor 
    assignRates(); 
} 
+0

我實際上改變了一些,因爲它說:'Rates':成員名稱不能與它們的封閉類型相同。 – programmerNOOB 2012-04-12 13:28:33

+0

是的,會員名不能。所以你不能有一個叫rate的方法,即:public void Rates(){assignRates(); }'。但是,這不是我們上面的。因爲沒有返回類型(在我的例子中是'void'),所以這是一個叫做構造函數的特殊方法。其中,*必須*與班級名稱相同。你可以試試上面的代碼嗎? – yamen 2012-04-12 20:39:02

相關問題