每次我运行程序时,它都会毫无问题地通过前四个纳税人,但是当它应该要求我提供第五个纳税人信息时,它就会停止。我整个上午都在寻找解决方案。我做错了什么或错过了什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApp
{
class Rates
{
public readonly int incomeLimit;
public readonly double lowTax;
public readonly double highTax;
public Rates()
{
incomeLimit = 30000;
lowTax = .15;
highTax = .28;
}
public Rates(int limit, double lowRate, double highRate)
{
limit = incomeLimit;
lowRate = lowTax;
highRate = highTax;
}
public double CalcTax(double income)
{
double tax;
if (income < incomeLimit)
tax = income * lowTax;
else
tax = income * highTax;
return tax;
}
}
class Taxpayer : IComparable
{
public string SSN { get; set; }
public double grossIncome { get; set; }
public double taxOwed
{
get
{
return taxOwed;
}
}
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;
}
public static void getRates()
{
Taxpayer tax = new Taxpayer();
int limit = 0;
double lowRate = 0;
double highRate = 0;
char input;
Console.Write("Do you want default values (enter D) or enter your own (enter O)?");
input = Char.ToUpper(Convert.ToChar(Console.ReadLine()));
switch (input)
{
case 'D':
Rates def = new Rates();
limit = def.incomeLimit;
lowRate = def.lowTax;
highRate = def.highTax;
def.CalcTax(tax.grossIncome);
break;
case 'O':
Rates own = new Rates(limit, lowRate, highRate);
Console.Write("Enter the dollar limit ");
limit = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the low rate ");
lowRate = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the high rate ");
highRate = Convert.ToDouble(Console.ReadLine());
own.CalcTax(tax.grossIncome);
break;
}
}
}
class Program
{
static void Main(string[] args)
{
Rates taxRates = new Rates();
Taxpayer[] taxarray = new Taxpayer[5];
for (int x = 1; x < taxarray.Length; ++x)
{
taxarray[x] = new Taxpayer();
Console.Write("Enter Social Security Number for taxpayer {0}: ", x);
taxarray[x].SSN = Console.ReadLine();
Console.Write("Enter gross income for taxpayer {0}: ", x);
taxarray[x].grossIncome = Convert.ToDouble(Console.ReadLine());
Taxpayer.getRates();
}
for (int i = 0; i < taxarray.Length; i++)
{
Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", taxarray[i].SSN, taxarray[i].grossIncome, taxRates.CalcTax(taxarray[i].grossIncome));
}
Array.Sort(taxarray);
Console.WriteLine("-------------------------------------------------------");
for (int i = 0; i < taxarray.Length; i++)
{
Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", taxarray[i].SSN, taxarray[i].grossIncome, taxRates.CalcTax(taxarray[i].grossIncome));
}
}
}
}