每次我运行程序时,它都会通过前四个纳税人没有问题,但是当它应该向我询问第五个纳税人信息时,它会停止。我整个上午都在寻找解决方案。我做错了什么,或者错过了什么?
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));
}
}
}
}
答案 0 :(得分:2)
Taxpayer[] taxarray = new Taxpayer[5];
for (int x = 1; x < taxarray.Length; ++x)
数组在C#中基于零。从1开始,&lt; 5是{1,2,3,4} = 4纳税人。
你应该从零开始:
for (int x = 0; x < taxarray.Length; x++)
另外:不要打扰++x
vs x++
,除非您关心操作的结果(在此示例中不是这样)。让JIT担心这种微观优化。
答案 1 :(得分:0)
此行从索引1开始
for (int x = 1; x < taxarray.Length; ++x)
数组从索引0开始 因此,您的输入代码应更改为
for (int x = 0; x < taxarray.Length; x++)
{
taxarray[x] = new Taxpayer();
Console.Write("Enter Social Security Number for taxpayer {0}: ", x+1);
taxarray[x].SSN = Console.ReadLine();
Console.Write("Enter gross income for taxpayer {0}: ", x+1);
taxarray[x].grossIncome = Convert.ToDouble(Console.ReadLine());
Taxpayer.getRates();
}
从索引0开始,并询问输入x + 1不会混淆用户
答案 2 :(得分:0)
实际上我认为从来没有要求你为第零纳税人。 C#数组基于0,您从1开始。