English 中文(简体)
在发送一个阵列时,由于某种方法而使某一数值回归,而该方案在使用时有误。
原标题:Having trouble returning a value from a method call when sending an array and the program is error out when run in reference to the sort

在该方案实施时,我取得了以下成果:

Please enter the Social Security Number for taxpayer 0:  111111111
Please enter the gross income for taxpayer 0:  20000
Please enter the Social Security Number for taxpayer 1:  555555555
Please enter the gross income for taxpayer 1:  50000
Please enter the Social Security Number for taxpayer 2:  333333333
Please enter the gross income for taxpayer 2:  5464166
Please enter the Social Security Number for taxpayer 3:  222222222
Please enter the gross income for taxpayer 3:  645641
Please enter the Social Security Number for taxpayer 4:  444444444
Please enter the gross income for taxpayer 4:  29000
Taxpayer # 1 SSN: 111111111, Income is $20,000.00, Tax is $0.00
Taxpayer # 2 SSN: 555555555, Income is $50,000.00, Tax is $0.00
Taxpayer # 3 SSN: 333333333, Income is $5,464,166.00, Tax is $0.00
Taxpayer # 4 SSN: 222222222, Income is $645,641.00, Tax is $0.00
Taxpayer # 5 SSN: 444444444, Income is $29,000.00, Tax is $0.00

Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable.
at System.Collections.Comparer.Compare(Object a, Object b)
at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
at System.Collections.Generic.ArraySortHelper`1.SwapIfGreaterWithItems(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
at System.Collections.Generic.ArraySortHelper`1.QuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer)
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
--- End of inner exception stack trace ---
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer) at System.Array.Sort[T](T[] array)
at Assignment5.Taxpayer.Main(String[] args) in Program.cs:line 150

是否在税额到期时通知零?

该守则是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace taxes
{
    class Rates
    {
        // Create a class named rates that has the following data members: 
        private int incLimit;
        private double lowTaxRate;
        private double highTaxRate;

        // use read-only accessor
        public int IncomeLimit  
        { get { return incLimit; } }
        public double LowTaxRate 
        { get { return lowTaxRate; } }
        public double HighTaxRate 
        { get { return highTaxRate; } }

        //A class constructor that assigns default values 
        public Rates()
        {
            int limit = 30000;
            double lowRate = .15;
            double highRate = .28;
            incLimit = limit;
            lowTaxRate = lowRate;
            highTaxRate = highRate;
        }
        //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
        public Rates(int limit, double lowRate, double highRate)
        {
        }
        //  A CalculateTax method that takes an income parameter and computes the tax as follows:
        public int CalculateTax(int income)
        {
            int limit = 0;
            double lowRate = 0;
            double highRate = 0;
            int taxOwed = 0;
            //  If income is less than the limit then return the tax as income times low rate.
            if (income < limit)
                taxOwed = Convert.ToInt32(income * lowRate);
            //  If income is greater than or equal to the limit then return the tax as income times high rate.
            if (income >= limit)
                taxOwed = Convert.ToInt32(income * highRate);
            return taxOwed;
        }


    }  //end class Rates

        //  Create a class named Taxpayer that has the following data members:
        public class Taxpayer
        {
            //Use get and set accessors.
            string SSN
            { get; set; }
            int grossIncome 
            { get; set; }

            //  Use read-only accessor.
            public int taxOwed  
            {
                get { return taxOwed; }
            }

            //   The Taxpayer class should be set up so that its objects are comparable to each other based on tax owed.
            class taxpayer : IComparable
            {
                public int taxOwed { get; set; }
                public int income { get; set; }
                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;

                }  // End IComparable.CompareTo
            } //end taxpayer  IComparable class
                //  **The tax should be calculated whenever the income is set.
                //  The Taxpayer class should have a getRates class method that has the following.
                public static void GetRates()
                {
                    //  Local method data members for income limit, low rate and high rate.
                    int incLimit = 0;
                    double lowRate;
                    double highRate;
                    string userInput;
                    //  Prompt the user to enter a selection for either default settings or user input of settings.
                    Console.Write("Would you like the default values (D) or would you like to enter the values (E)?:  ");
                    /*   If the user selects default the default values you will instantiate a rates object using the default constructor
                    * and set the Taxpayer class data member for tax equal to the value returned from calling the rates object CalculateTax method.*/
                    userInput = Convert.ToString(Console.ReadLine());
                    if (userInput == "D" || userInput == "d")
                    {
                        Rates rates = new Rates();
                        rates.CalculateTax(incLimit);
                    } // end if
                    /*  If the user selects to enter the rates data then prompt the user to enter values for income limit, low rate and high rate, 
                     * instantiate a rates object using the three-argument constructor passing those three entries as the constructor arguments and 
                     * set the Taxpayer class data member for tax equal to the valuereturned from calling the rates object CalculateTax method. */
                    if (userInput == "E" || userInput == "e")
                    {
                        Console.Write("Please enter the income limit: ");
                        incLimit = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Please enter the low rate: ");
                        lowRate = Convert.ToDouble(Console.ReadLine());
                        Console.Write("Please enter the high rate: ");
                        highRate = Convert.ToDouble(Console.ReadLine());
                        Rates rates = new Rates(incLimit, lowRate, highRate);
                        rates.CalculateTax(incLimit);
                    }  
                }  

            static void Main(string[] args)
            {

                Taxpayer[] taxArray = new Taxpayer[5];
                Rates taxRates = new Rates();
                //  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
                for (int x = 0; x < taxArray.Length; ++x)
                {
                    taxArray[x] = new Taxpayer();
                    Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x + 1);
                    taxArray[x].SSN = Console.ReadLine();

                    Console.Write("Please enter the gross income for taxpayer {0}:  ", x + 1);
                    taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine());

                }

                Taxpayer.GetRates();

                //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
                for (int i = 0; i < taxArray.Length; ++i)
                {
                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome));

                } // end for
                //  Implement a for-loop that will sort the five objects in order by the amount of tax owed 
                Array.Sort(taxArray);
                Console.WriteLine("Sorted by tax owed");
                for (int i = 0; i < taxArray.Length; ++i)
                {
                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome));

                }
            }  //end main
        } //  end Taxpayer class

}  //end 

对于美元数额为何升值零以及为何不奏效,有何影响?

问题回答
  • 页: 1

  • <编码> 纳税人.taxOwed是无限的收回。

  • 我不认为需要打上<条码>。

    userInput = Convert.ToString(Console.ReadLine());
    
  • You never use the result of calling Rates.CalculateTax.

  • 这与<代码>Rates的任何事物无关;它似乎没有任何作用。

  • 表格<代码>tax Payer 似乎没有用于任何东西。 您是否试图制定<条码>塔克帕特>。 页: 1 页: 1 或许你也希望执行<条码>IComparable<Tax Payer>。

  • 你正在利用你的方案类别来获取数据。 您或许应当将<代码>Tax Payer与您的方案类别分开。 它应当是一个单独的类别,如<代码>Rates。

Rates rates = new Rates(incLimit, lowRate, highRate);
                        rates.CalculateTax(incLimit);

如果没有执行,你就会向建筑商出售价值。

//A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
        public Rates(int limit, double lowRate, double highRate)
        {
        }

assign the values to the methods inside this constructor and try running the program, Try to debug and tell us where you are facing the problem.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签