2012-04-12 6 views
1

문제의 메서드 호출을 올바른 값을 얻는 것은 결국 여기메소드 호출에 문제가 다시

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)); 

에서이를 WriteLine 호출 내 것은 방법이

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; 

는 이런 경우 사용자는 여기에서 나머지이며, 다른 아무것도 볼 필요가있다.

내가 너무 정렬에 대한 질문이 있는데, 내가 제대로 taxuwed 금액으로 정렬해야하지만 그것은 현재 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-tax-at-taxamount/10116729 # 10116729 –

답변

0

문제는 기본 요금을 설정하는 코드를 호출하지 않는다는 것입니다. 이 코드는 :

는 '생성자'로 주석 있지만
//A class constructor that assigns default values 
public void assignRates() 
{ 
    incLimit = 30000; 
    lowTaxRate = .15; 
    highTaxRate = .28; 
} 

아니다. 결코 부름을 받거나 실행되지 않습니다. 내가 u는이 질문을 생각

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

'요금': 회원 이름은 동봉 된 유형과 같을 수 없습니다. – programmerNOOB

+0

예, 회원 이름을 사용할 수 없습니다. 그래서 당신은 rates라고하는 메소드를 가질 수 없습니다 : public void Rates() {assignRates(); }'. 그러나 그것은 우리가 위에있는 것이 아닙니다. 리턴 타입이 없기 때문에 (예 :'void') 이것은 생성자라고하는 특별한 메소드입니다. 사실, 수업과 같은 이름을 지어야합니다. 위의 코드를 사용해 주시겠습니까? – yamen

관련 문제