0

プログラムを実行するたびに、最初の 4 つの納税者を問題なく通過しますが、5 番目の納税者の情報を要求すると停止します。私は午前中ずっと解決策を探していました。私は何が間違っているか、または欠けていますか?

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を気にしないでください(この例では気にしません)。このようなマイクロ最適化については JIT に任せてください。x++

于 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 に答える