输出对象数组时出现问题
本文关键字:问题 对象 数组 输出 | 更新日期: 2023-09-27 18:24:49
我正在编写这个程序,无法显示两个项目。紫杉变量在每个输出中显示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();
}
}
}
使用调试器,进入这一行:tpArray[x].CalcTax();
,您会看到问题:您没有使用用户输入的速率,而是创建了一个新的Rates
对象用于计算。
当然,这个新的Rates
对象没有初始化,并且速率都是零。
另一个问题是,为了输出纳税人#,您应该输出x
,而不是tpArray[x]