0
이 프로그램을 작성 중이며 표시 할 항목을 두 개 가져올 수 없습니다. taxowed 변수는 각 출력에 0을 표시하며 납세자 번호는 출력에 전혀 표시되지 않습니다 ... 누군가가 왜 이런 일이 발생하는지 설명 할 수 있습니까? tpArray[x].CalcTax();
을하고 문제를 볼 수 있습니다 :개체 배열 출력에 문제가 있습니다.
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();
}
}
}
도와 주셔서 감사합니다 Zruty의 출력
x
, 내 읽기 전용이 값을 TaxOwed 제공하는 CalcTax 방법을 만들어야했습니다해야, 출력하기 위해, 납세자 번호입니다. TaxOwed 변수에 누락 된 값을 입력하는 또 다른 방법이 있습니까? – Joe여러 가지 방법으로 문제를 해결할 수 있습니다. 더 높은 수준의 그림을보아야합니다 : 왜'Rates' 객체가 필요합니까? 왜 'TaxPayer' 오브젝트가 필요한가요? 그런 다음 제대로 연결하는 방법 알아보기 – Zruty