从调用计算 taxAmount 的方法中只能获得 $0
本文关键字:计算 调用 taxAmount 方法 | 更新日期: 2023-09-27 18:30:53
我在以下调用中遇到问题,特别是最后一个组件:
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));
我正在调用 Rates 类中的 CalculateTax 方法,该方法在 main 中作为 taxRates 启动。
这是计算税收方法
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 if(income >= incLimit) {
taxOwed = Convert.ToInt32(income * highTaxRate);}
else taxOwed = 0;
return taxOwed;
}
incLimit,LowTaxRate 和 highTaxRate 是之前设置的
任何想法为什么总是出来到0。 我什至向该方法发送了一个数字,例如 50000,但仍然返回 0。
我可以仅使用该方法本身获取一个值,因此它是其他东西,这是代码
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()
{
//int limit = 30000;
//double lowRate = .15;
//double highRate = .28;
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);
Console.WriteLine(taxOwed);
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());
}
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(50000));//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
无复制。使用下面的简单程序,我得到了有效的非零结果(900
使用我的值):
internal class Program {
private static int incLimit = 30000;
private static float lowTaxRate = 0.18F;
private static float highTaxRate = 0.30F;
private static void Main(string[] args) {
var result = CalculateTax(5000);
}
public static 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 if (income >= incLimit) {
taxOwed = Convert.ToInt32(income * highTaxRate);
}
else taxOwed = 0;
return taxOwed;
}
}
你的问题在于你对 Rates.assignRates() 方法的使用。 您只能从静态 Taxpayer.GetRates() 方法调用它。 此方法作用于本地 Rates 对象,然后丢弃填充的对象。 您可能希望更改 Taxpayer.GetRates() 以返回 Rates 对象,返回内部创建(和填充)的 rate 变量:
public static Rates GetRates() { ... return rates; }
然后在 Main() 中,删除对 Taxpayer.GetRates() 的现有调用,并更改声明 taxRates 变量的行,如下所示:
Rates taxRates = Taxpayer.GetRates();
另请注意,您还应该以某种方式处理由于输入错误/丢失而导致的错误情况,但您现在似乎没有这样做,因此除了让您返回填充的 Rates 对象之外,我没有包含任何功能更改。
此外,您可能需要考虑将 Rates 类设置为静态,因为您似乎在整个过程中只使用它的一个实例。
你的最后一个决策点else taxOwed = 0;
永远不会执行,所以不需要它。我已经运行了如下所示的代码,一切正常。问题一定在于传递给方法的参数为零,或者您没有像您认为的那样设置值。
void Main()
{
var result = CalculateTax(40000);
Console.WriteLine(result);
}
public int CalculateTax(int income)
{
var incLimit = 50000;
var lowTaxRate = 0.10;
var highTaxRate = 0.25;
int taxOwed;
if (income < incLimit){
taxOwed = Convert.ToInt32(income * lowTaxRate); }
else if(income >= incLimit) {
taxOwed = Convert.ToInt32(income * highTaxRate);}
return taxOwed;
}
更新
现在你已经发布了完整的代码,你的问题是你需要改变静态GetRates()
方法来返回速率,正如Clark提到的。该静态方法是调用rates.assignRates()
的唯一位置,并且这些分配的速率仅适用于该方法中包含的特定rates
实例,而不适用于其他任何地方。因此,将GetRates()
更改为返回rates
实例,如下所示:
public static Rates GetRates()
{
...
Rates rates = new Rates();
...
return rates;
}
然后更改主方法,如下所示:
static void Main(string[] args)
{
Taxpayer[] taxArray = new Taxpayer[5];
// Implement a for-loop that will prompt the user to enter
// the Social Security Number and gross income.
...
Rates taxRates = 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(50000));
}
...
}
我会在您的方法中添加调试语句以确认 lowTaxRate 和 hightTaxRate 不是零/空。
您确定 lowTaxRate 和 highTaxRate 未设置为 0,因为任何乘以 0 的东西都是 0。放置一些调试器/消息框来检查这一点。