0

每次我运行程序时,它都会毫无问题地通过前四个纳税人,但是当它应该要求我提供第五个纳税人信息时,它就会停止。我整个上午都在寻找解决方案。我做错了什么或错过了什么?

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

    }
}
}
4

3 回答 3

2
    Taxpayer[] taxarray = new Taxpayer[5];
    for (int x = 1; x < taxarray.Length; ++x)

数组在 C# 中从零开始。从 1 开始且 < 5 是 {1,2,3,4} = 4 个纳税人。

你应该从零开始:

    for (int x = 0; x < taxarray.Length; x++)

另外:不要打扰++xvs x++,除非你关心操作的结果(在这个例子中你不关心)。让 JIT 担心这种微优化。

于 2013-04-09T20:21:57.620 回答
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 以免混淆用户

于 2013-04-09T20:22:01.347 回答
0

其实我认为它从来没有向你要第零个纳税人。C# 数组是基于 0 的,而您从 1 开始。

于 2013-04-09T20:22:15.027 回答