有麻烦的方法调用,并得到一个正确的值回 [英] Having trouble with a method call and getting a correct value back

查看:90
本文介绍了有麻烦的方法调用,并得到一个正确的值回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的方法调用是这样的WriteLine调用中在最后

  Console.WriteLine(纳税人#{0} SSN:{1},收入是{2:C},税{3:C}我+ 1,taxArray [I] .SSN,taxArray [I] .grossIncome,taxRates.CalculateTax(taxArray [I] .grossIncome));

下面是费率类其中该方法是在

 公共类价格
{
    //创建一个名为类价格具有下列数据成员:
    INT incLimit;
    双lowTaxRate;
    双highTaxRate;    //使用只读访问
    公众诠释IncomeLimit
    {{返回incLimit; }}
    公共双LowTaxRate
    {{返回lowTaxRate; }}
    公共双HighTaxRate
    {{返回highTaxRate; }}    //类的构造函数,分配缺省值
    公共无效assignRates()
    {
        incLimit = 30000;
        lowTaxRate = 0.15;
        highTaxRate = 0.28;
    }
    //类的构造函数,有三个参数指定的输入值的极限,低利率和高利率。
    公共无效assignRates(INT廉,双低,双高)
    {
        incLimit = LIM;
        lowTaxRate =低;
        highTaxRate =高;
    }
    //一个calculateTax的方法,它的收入参数,并计算纳税如下:
    公众诠释calculateTax的(INT收入)
    {        INT taxOwed;
        //如果收入低于极限然后返回税收收入倍率低。
        如果(收入和LT; incLimit)
            taxOwed = Convert.ToInt32(收入* lowTaxRate);
        //如果收入大于或等于该极限,然后返回该税作为收入倍率高。
        其他
            taxOwed = Convert.ToInt32(收入* highTaxRate);        返回taxOwed;
    }

现在,如果我坚持在以下变量值初始化时,我可以得到一个返回值,但如果我离开他们的默认,我总是从获得的WriteLine 0。

  INT incLimit;
双lowTaxRate;
双highTaxRate;

万一你需要看别的,这里是休息。

我有一个关于这类问题也一样,我不认为这是正常工作,因为我需要它由taxOwed量排序但目前0和我的问题。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间Assignment5_2
{公共类价格
{
    //创建一个名为类价格具有下列数据成员:
    INT incLimit;
    双lowTaxRate;
    双highTaxRate;    //使用只读访问
    公众诠释IncomeLimit
    {{返回incLimit; }}
    公共双LowTaxRate
    {{返回lowTaxRate; }}
    公共双HighTaxRate
    {{返回highTaxRate; }}    //类的构造函数,分配缺省值
    公共无效assignRates()
    {
        incLimit = 30000;
        lowTaxRate = 0.15;
        highTaxRate = 0.28;
    }
    //类的构造函数,有三个参数指定的输入值的极限,低利率和高利率。
    公共无效assignRates(INT廉,双低,双高)
    {
        incLimit = LIM;
        lowTaxRate =低;
        highTaxRate =高;
    }
    //一个calculateTax的方法,它的收入参数,并计算纳税如下:
    公众诠释calculateTax的(INT收入)
    {        INT taxOwed;
        //如果收入低于极限然后返回税收收入倍率低。
        如果(收入和LT; incLimit)
            taxOwed = Convert.ToInt32(收入* lowTaxRate);
        //如果收入大于或等于该极限,然后返回该税作为收入倍率高。
        其他
            taxOwed = Convert.ToInt32(收入* highTaxRate);        返回taxOwed;
    }
} //结束类价格//创建一个名为纳税人类具有下列数据成员:
公共类纳税人:IComparable的
{
    //使用get和set访问。
    串SSN
    {设置;得到; }
    INT grossIncome
    {设置;得到; }
    INT taxOwed
    {设置;得到; }    INT IComparable.CompareTo(对象o)
    {
        INT returnVal;
        纳税人临时=(纳税人)O;
        如果(this.taxOwed> temp.taxOwed)
            returnVal = 1;
        否则,如果(this.taxOwed< temp.taxOwed)
            returnVal = -1;
        否则returnVal = 0;        返回returnVal;    } //结束IComparable.CompareTo    公共静态无效GetRates()
    {
        //本地方法的数据成员的入息限额,低利率和高利率。        INT incLimit;
        双lowRate;
        双highRate;
        串userInput;
        价格比率=新价格();
        //提示用户输入一个选择为任一缺省设置或设置的用户输入。
        Console.Write(你想默认值(D),或者你想输入值(E)?:);
        / *如果用户选择默认的缺省值,你将实例使用默认的构造函数对象率
        *并为税收等于从调用率返回的值纳税人类的数据成员对象calculateTax的方法。* /
        userInput =(到Console.ReadLine());
        如果(userInput ==D|| userInput ==D)
        {            rates.assignRates();
        } // 万一
        / *如果用户选择进入,然后率数据提示用户的入息限额,低利率和高利率输入值,
         *实例化一个对象率使用的三参数构造函数传递这三个条目的构造函数的参数和
         *设定等于从调用率valuereturned对象calculateTax的方法税纳税人类的数据成员。 * /
        否则,如果(userInput ==E|| userInput ==E)
        {
            Console.Write(请输入入息限额:);
            incLimit = Convert.ToInt32(到Console.ReadLine());
            Console.Write(请输入率低:);
            lowRate = Convert.ToDouble(到Console.ReadLine());
            Console.Write(请输入率高:);
            highRate = Convert.ToDouble(到Console.ReadLine());
            //价格比率=新价格();
            rates.assignRates(incLimit,lowRate,highRate);
        }
        其他Console.WriteLine(你做出了不正确的选择);
    }    静态无效的主要(字串[] args)
    {        纳税人[] = taxArray新纳税人[5];
        价格taxRates =新价格();
        //实现一个for循环,将提示用户输入社会安全号码和总收入。
        为(中间体X = 0; X&下; taxArray.Length ++ x)的
        {
            taxArray [X] =新纳税人();
            Console.Write(请输入社会安全号码纳税人{0}:X + 1);
            taxArray [X] .SSN =到Console.ReadLine();            Console.Write(请输入纳税人的收入总额{0}:X + 1);
            taxArray [X] .grossIncome = Convert.ToInt32(到Console.ReadLine());
            taxArray [X] .taxOwed = taxRates.CalculateTax(taxArray [X] .grossIncome);        }
        Taxpayer.GetRates();        //实现一个for循环,将显示每个对象格式化纳税人SSN,收入和计算的税款。
        的for(int i = 0; I< taxArray.Length ++ I)
        {
            Console.WriteLine(纳税人#{0} SSN:{1},收入是{2:C},税{3:C}我+ 1,taxArray [I] .SSN,taxArray [I] .grossIncome ,taxArray [I]); // taxRates.CalculateTax(taxArray [I] .grossIncome));        } //结束了
        //实现一个for循环,将通过税收欠款,​​以五个对象进行排序
        的Array.Sort(taxArray);
        Console.WriteLine(所欠税收排序);
        的for(int i = 0; I< taxArray.Length ++ I)
        {
            Console.WriteLine(纳税人#{0} SSN:{1},收入是{2:C},税{3:C}我+ 1,taxArray [I] .SSN,taxArray [I] .grossIncome ,taxRates.CalculateTax(taxArray [I] .grossIncome));        }
    } //主到底} //结束纳税人类} //结束


解决方案

的问题是,你不调用code,设置的违约率。这code:

  //一个类构造函数分配缺省值
公共无效assignRates()
{
    incLimit = 30000;
    lowTaxRate = 0.15;
    highTaxRate = 0.28;
}

虽然评价为构造不是的。它从来没有被调用或运行。由于类被称为价格,你需要一个默认的构造函数叫做价格:

 公开价格()
{
    //分配违约率在构造函数
    assignRates();
}

the method call in question is within this writeline call at the end

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

Here is the rates class which the method is in

public class Rates
{
    // Create a class named rates that has the following data members: 
    int     incLimit;
    double lowTaxRate;
    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 void assignRates()
    {
        incLimit = 30000;
        lowTaxRate = .15;
        highTaxRate = .28;
    }
    //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
    public void assignRates(int lim, double low, double high)
    {
        incLimit = lim;
        lowTaxRate = low;
        highTaxRate = high;
    }
    //  A CalculateTax method that takes an income parameter and computes the tax as follows:
    public int CalculateTax(int income)
    {

        int taxOwed;
        //  If income is less than the limit then return the tax as income times low rate.
        if (income < incLimit)
            taxOwed = Convert.ToInt32(income * lowTaxRate); 
        //  If income is greater than or equal to the limit then return the tax as income times high rate.
        else 
            taxOwed = Convert.ToInt32(income * highTaxRate);

        return taxOwed;
    }

Now if I stick a value in the following variables when they are initialized, I can get a value returned but if I leave them as default, I always get 0 from the writeline.

int     incLimit;
double lowTaxRate;
double highTaxRate;

Just in case you need to see anything else, here is the rest.

I have a question about the sort too, I don't think it is working correctly as I need it to sort by the taxOwed amount but that is currently 0 and my problem.

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

namespace Assignment5_2
{

public class Rates
{
    // Create a class named rates that has the following data members: 
    int     incLimit;
    double lowTaxRate;
    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 void assignRates()
    {
        incLimit = 30000;
        lowTaxRate = .15;
        highTaxRate = .28;
    }
    //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
    public void assignRates(int lim, double low, double high)
    {
        incLimit = lim;
        lowTaxRate = low;
        highTaxRate = high;
    }
    //  A CalculateTax method that takes an income parameter and computes the tax as follows:
    public int CalculateTax(int income)
    {

        int taxOwed;
        //  If income is less than the limit then return the tax as income times low rate.
        if (income < incLimit)
            taxOwed = Convert.ToInt32(income * lowTaxRate); 
        //  If income is greater than or equal to the limit then return the tax as income times high rate.
        else 
            taxOwed = Convert.ToInt32(income * highTaxRate);

        return taxOwed;
    }


}  //end class Rates

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

    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

    public static void GetRates()
    {
        //  Local method data members for income limit, low rate and high rate.

        int incLimit;
        double lowRate;
        double highRate;
        string userInput;
        Rates rates = new Rates();
        //  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 = (Console.ReadLine());
        if (userInput == "D" || userInput == "d")
        {

            rates.assignRates();
        } // 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. */
        else 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();
            rates.assignRates(incLimit, lowRate, highRate);
        }
        else Console.WriteLine("You made an incorrect choice");
    }

    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());
            taxArray[x].taxOwed = taxRates.CalculateTax(taxArray[x].grossIncome);

        }


        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, taxArray[i]);//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 

解决方案

The problem is that you're not calling the code that sets the default rates. This code:

//A class constructor that assigns default values 
public void assignRates()
{
    incLimit = 30000;
    lowTaxRate = .15;
    highTaxRate = .28;
}

Although commented as a 'constructor' isn't one. It never gets called or runs. Since your class is called Rates, you need a default constructor called Rates:

public Rates()
{
    // assign the default rates in your constructor
    assignRates();
}

这篇关于有麻烦的方法调用,并得到一个正确的值回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆