【问题标题】:Unable to use new class constructor values that would overwrite default values in C#无法使用会覆盖 C# 中的默认值的新类构造函数值
【发布时间】:2015-02-27 21:20:47
【问题描述】:

我编写了一些代码来获取五个社会安全号码、五个总收入条目,并为每个条目计算税金。该程序将使用美元限额、低税率和高税率的默认值来计算所欠税款。但它也会让用户选择是否要使用自己的美元限额、低税率和高税率来计算所选纳税人的欠税。

问题: 每当我输入自己的美元限额、低税率和高税率来计算欠税时,它仍然使用默认值(限制 = 30000,低税率 = .15 和高税率 = .28)。这就是如何计算出错误的欠税值。

问题。 可能是因为我的“public static void GetRates(int income)”没有返回类型(它是无效的)吗?在我调用“Taxpayer.GetRates(taxPayer[x].grossIncome);”之后,我是否应该将一个值返回给 main方法?

部分代码

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

namespace assignment
{
    public class Rates
    {
        public int incomeLimit; // income limit
        public double lowTaxRate; // low tax rate
        public double highTaxRate; // high tax rate

        public int IncomeLimit // read only property
        {
            get
            {
                return incomeLimit;
            }
        }
        public double LowTaxRate // read only property
        {
            get
            {
                return lowTaxRate;
            }
        }
        public double HighTaxRate // read only property
        {
            get
            {
                return highTaxRate;
            }
        }

        public Rates()
        {
            DoRates();
        }

        public void DoRates() // class constructor that assigns default values 
        {
            // set default values for limit, lowRate, and highRate
            incomeLimit = 30000;
            lowTaxRate = .15;
            highTaxRate = .28;
        }

        public void DoRates(int limit, double lowRate, double highRate) // class constructor that takes three parameters
        {
            incomeLimit = limit; 
            lowTaxRate = lowRate;
            highTaxRate = highRate;
        }
        // CalculateTax method that takes an income parameter and computes the tax
        public int CalculateTax(int income)
        {
            int taxOwed = 0;

            if (income < incomeLimit)
            {
                taxOwed = Convert.ToInt32(income * lowTaxRate);

            }
            if (income >= incomeLimit)
            {
                taxOwed = Convert.ToInt32(income * highTaxRate);

            }
            return taxOwed;
        }  
    }
    public class Taxpayer : IComparable
    {
        string socialSecurityNum;
        int grossIncome;
        int taxOwed;

        // Use get and set accessors.
        public string SocialSecurityNum
        {
            get
            {
                return socialSecurityNum;
            }
            set
            {
                socialSecurityNum = value;
            }
        }
        // Use get and set accessors.
        public int GrossIncome
        {
            get
            {
                return grossIncome;
            }
            set
            {
                grossIncome = value;
            }
        }
        //  Use read-only accessor
        public int TaxOwed
        {
            get
            {
                return taxOwed;
            }
        }
        // objects are comparable to each other based on tax owed.
        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(int income)
        {

            int incomeLimit;
            double lowRate;
            double highRate;

            char input;
            Rates rates = new Rates();

            Console.Write("Do you want default values (enter D) or enter your own (enter O)? ");
            input = Convert.ToChar(Console.ReadLine());


            switch (char.ToUpper(input)) // start switch
            {        
                case 'D': // if the input latter is d or a D
                    rates.DoRates();
                    break;
                case 'O': // if the input latter is o or an O
                    Console.Write("Enter the dollar limit ");
                    incomeLimit = 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());

                    rates.DoRates(incomeLimit, lowRate, highRate);
                    rates.CalculateTax(income);
                    break;
                default: Console.WriteLine("You entered and incorrect option"); // display this messages if the input was something other than D or O
                    break;
            }


        }

        public static void Main()
        {
            // instantiate an array of five (5) Taxpayer objects.
            Taxpayer[] taxPayer = 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 < taxPayer.Length; ++x)
            {

                taxPayer[x] = new Taxpayer();
                Console.Write("Enter Social Security Number for taxpayer {0} ", x + 1);
                taxPayer[x].socialSecurityNum = Convert.ToString(Console.ReadLine());


                Console.Write("Enter gross income for taxpayer {0} ", x + 1);
                taxPayer[x].grossIncome = Convert.ToInt32(Console.ReadLine());

                Taxpayer.GetRates(taxPayer[x].grossIncome);


                taxPayer[x].taxOwed = taxRates.CalculateTax(taxPayer[x].grossIncome);

            }

感谢大家的帮助。我想我在编写这段代码时确实有点得意忘形。在将代码与我的问题隔离后,我终于弄明白了。如果有人想看,我会这样做。

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

namespace assignment
{
    public class Rates
    {
        public int incomeLimit; // income limit
        public double lowTaxRate; // low tax rate
        public double highTaxRate; // high tax rate

        public int IncomeLimit { get { return incomeLimit; } }// read only property
        public double LowTaxRate { get { return lowTaxRate; } } // read only property
        public double HighTaxRate { get { return highTaxRate; } }// read only property

        public Rates()
        {
            incomeLimit = 30000;
            lowTaxRate = .15;
            highTaxRate = .28;
        }

        public Rates(int incomeLim, double lowRate, double highRate)
        {   
            incomeLimit = incomeLim;
            lowTaxRate = lowRate;
            highTaxRate = highRate;

        }

        // CalculateTax method that takes an income parameter and computes the tax
        public int CalculateTax(int income)
        {
            int taxOwed = 0;

            if (income < incomeLimit)
            {
                taxOwed = Convert.ToInt32(income * lowTaxRate);
            }
            if (income >= incomeLimit)
            {
                taxOwed = Convert.ToInt32(income * highTaxRate);
            }
            return taxOwed;
        }
    }
    public class Taxpayer
    {
        string socialSecurityNum = null;
        int grossIncome = 0;
        int taxOwed = 0;

        // Use get and set accessors.
        public string SocialSecurityNum { get {return socialSecurityNum;} set {socialSecurityNum = value;} }
        // Use get and set accessors.
        public int GrossIncome { get { return grossIncome; } set { grossIncome = value; } }
        //  Use read-only accessor
        public int TaxOwed { get { return taxOwed; } }

        public void GetRates(int income)
        {
            int incomeLimit = 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 = Convert.ToChar(Console.ReadLine());


            switch (char.ToUpper(input)) // start switch
            {
                case 'D': // if the input latter is d or a D
                    Rates rates = new Rates();
                    taxOwed = rates.CalculateTax(income);
                    break;
                case 'O': // if the input latter is o or an O
                    Console.Write("Enter the dollar limit ");
                    incomeLimit = 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());
                    Rates myrates = new Rates(incomeLimit, lowRate, highRate);
                    taxOwed = myrates.CalculateTax(income);
                    break;
                default: Console.WriteLine("You entered and incorrect option"); // display this messages if the input was something other than D or O
                    break;
            }

        }
        public static void Main()
        {
            Taxpayer[] taxPayer = new Taxpayer[5];
            Rates taxRates = new Rates();

            Taxpayer myTaxpayer = new Taxpayer();
            // Implement a for-loop that will prompt the user 
            // to enter the Social Security Number and gross income.
            for (int x = 0; x < taxPayer.Length; ++x)
            {
                taxPayer[x] = new Taxpayer();
                Console.Write("Enter Social Security Number for taxpayer {0} ", x + 1);
                taxPayer[x].socialSecurityNum = Convert.ToString(Console.ReadLine());
                Console.Write("Enter gross income for taxpayer {0} ", x + 1);
                taxPayer[x].grossIncome = Convert.ToInt32(Console.ReadLine());
                myTaxpayer.GetRates(taxPayer[x].grossIncome);

                taxPayer[x].taxOwed = myTaxpayer.taxOwed;
            }
            // Implement a for-loop that will display each object 
            // as formatted taxpayer SSN, income and calculated tax.
            for (int y = 0; y < taxPayer.Length; ++y)
            {
                Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:C} Tax is {3:C}", y + 1, taxPayer[y].socialSecurityNum,
                    taxPayer[y].grossIncome, taxPayer[y].taxOwed);
            }
        }
    }
}

【问题讨论】:

  • 鉴于您从未更改过它,如何期望它会发生变化?
  • 这是一个真正很好的第一次尝试。不过,那是 的代码。你能缩小范围吗?您还有一些不好的做法,例如公共字段(当您将它们公开为只读属性时更奇怪)。
  • 好问题!只要您用minimal complete example 替换您的代码块,就会立即支持和帮助(如果您仍然需要它)。
  • 好的,谢谢大家。我会重写它,让它更短。
  • 此外,很少像这样计算税款,因为这意味着(以您的默认值为例)一旦扣除税款,35000 上的人将比30000 即 25200 (35000-9800) 与 25500 (30000-4500)。通常发生的情况是,只有超出限额的钱才以更高的税率征税,而低于限额的钱则按原始税率征税,例如35000 - (30000 * .15) - (5000 * .28) = 29100 - 这更有意义。

标签: c#


【解决方案1】:

我会建议你改变你的代码,更干净的代码并使用更好的 OOP 技术。

您应该检查 Taxpayer 类,特别是名为 GetRates() 的方法。

现在,如果您检查 Rates 类的构造函数,则在该方法中创建一个 Rates 类型的对象

public Rates()
    {
        DoRates();
    }

它调用了Dorates()方法,但没有参数,所以它总是会调用这些DoRates方法

public void DoRates() // class constructor that assigns default values 
    {
        // set default values for limit, lowRate, and highRate
        incomeLimit = 30000;
        lowTaxRate = .15;
        highTaxRate = .28;
    }

【讨论】:

    【解决方案2】:

    我很惊讶你的编译器没有抱怨。在Taxpayer.GetRates(int) 中,您调用Rates.CalculateTax(int),它返回一个int 值。它不会在任何地方存储或返回,所以我不确定调用的目的是什么。此外,由于对象rates 附加到静态方法GetRates(int),因此您不会更改用于计算税款的对象taxrates 中的任何值。最后,正如建议的那样,您的构造函数设置默认值。您可能会添加另一个构造函数来获取参数。从那里,您可能需要确保更改用于计算税款的对象中的值。

    奥卡姆剃刀:保持简单愚蠢。我认为您对 OOP 感到厌烦。简化一些事情以使其发挥作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 2011-03-19
      相关资源
      最近更新 更多