我写了一些代码来接受五个社会安全号码,五个总收入条目,并计算每个税收。该计划将使用美元限额,低利率和高利率的默认值来计算欠税。但它也会让用户选择是否要使用自己的美元限额,低利率和高利率来计算所选纳税人的欠税。
问题: 每当我输入我自己的美元限额,低利率和高利率来计算欠税时,它仍然使用默认值(限制= 30000,低利率= .15和高利率= .28)。这就是如何获得错误计算的税收欠款值。
问题即可。 可能是因为我的" public static void GetRates(int income)"没有返回类型(它是无效的)?我打电话给#34; 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);
}
}
}
}
答案 0 :(得分:1)
我建议您更改代码,更干净的代码并使用更好的OOP技术。
您应该检查类Taxpayer,特别是名为GetRates()的方法。
在该方法中,您创建了一个类型为Rates的对象,现在,如果您检查类Rate的构造函数
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;
}
答案 1 :(得分:0)
我很惊讶你的编译器并没有抱怨。在Taxpayer.GetRates(int)
中,您调用Rates.CalculateTax(int)
,它返回一个int值。它不会被存储或返回任何地方,因此我不确定该通话的目的是什么。此外,由于对象rates
附加到静态方法GetRates(int)
,因此您不会更改用于计算税额的对象taxrates
中的任何值。最后,正如所建议的那样,构造函数设置默认值。您可以添加另一个构造函数来获取参数。从那里,您可能希望确保更改您用于计算税额的对象中的值。
奥卡姆剃刀:保持简单愚蠢。我认为你会被OOP贬低。简化一些事情以使其发挥作用。