无法正确显示欠税结果

本文关键字:结果 显示 | 更新日期: 2023-09-27 18:08:26

一切似乎运行正常,除了显示的欠税总是0。我不知道如何正确地获得默认值或用户条目的计算。这里是代码,我有任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApp
{
class Rates
{
    public readonly int incomeLimit;
    public readonly double lowTax;
    public readonly double highTax;
    public Rates()
    {
        incomeLimit = 30000;
        lowTax = .15;
        highTax = .28;
    }
    public Rates(int limit, double lowRate, double highRate)
    {
        limit = incomeLimit;
        lowRate = lowTax;
        highRate = highTax;
    }
    public double CalcTax(double income)
    {            
        double tax; 
        if (income < incomeLimit)
        tax = income * lowTax;
        else
            tax = income * highTax;
        return tax;
    }
}
class Taxpayer : IComparable <Taxpayer>
{
    public string social;
    public double grossincome;
    public double taxowed;
    public string SSN 
    { 
        get
    {
        return social;
    }
        set
    {
        social = value;
    }
    }
    public double grossIncome 
    { 
        get
        {
            return grossincome;
        }
        set
        {
            grossincome = value;
        }
    }
    public double taxOwed
    {
        get
        {
            return taxowed;
        }
    }
    public int CompareTo(Taxpayer o)
    {           
        return this.taxOwed.CompareTo(o.taxOwed);
    }
    public static void getRates()
    {
        Rates rates = new Rates();
        Taxpayer tax = new Taxpayer();
        int limit = 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 = Char.ToUpper(Convert.ToChar(Console.ReadLine()));
        switch (input)
        {
            case 'D':
                Rates def = new Rates();
                limit = def.incomeLimit;
                lowRate = def.lowTax;
                highRate = def.highTax;
                tax.taxowed = def.CalcTax(tax.grossIncome);
                break;
            case 'O':
                Rates own = new Rates(limit, lowRate, highRate);
                Console.Write("Enter the dollar limit ");
                limit = 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());
                tax.taxowed = own.CalcTax(tax.grossIncome);
                break;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Taxpayer[] taxarray = new Taxpayer[5];
        for (int x = 0; x < taxarray.Length; ++x)
        {
            taxarray[x] = new Taxpayer();
            Console.Write("Enter Social Security Number for taxpayer {0}: ", x+1);
            taxarray[x].SSN = Console.ReadLine().Replace("-", "");
            Console.Write("Enter gross income for taxpayer {0}: ", x+1);
            taxarray[x].grossIncome = Convert.ToDouble(Console.ReadLine());
            //Taxpayer.getRates();
            Taxpayer.getRates();
        }
        Console.WriteLine();
        for (int i = 0; i < taxarray.Length; i++)
        {               
            Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", 
              i+1,taxarray[i].SSN, taxarray[i].grossIncome, taxarray[i].taxOwed);
        }
        Console.WriteLine("-------------------------------------------------------");
        Array.Sort(taxarray);
        for (int i = 0; i < taxarray.Length; i++)
        {
            Console.WriteLine("Taxpayer # {0} SSN: {1} income {2:c} Tax is {3:c}", 
              i+1,taxarray[i].SSN, taxarray[i].grossIncome, taxarray[i].taxOwed);
        }            
    }
}
}

无法正确显示欠税结果

主要问题似乎是您正在调用静态方法(getRates)。您输入的值将存储在该方法中新创建的TaxPayer实例中,但它们永远不会被复制到您在数组中创建的实例中。

我想你应该把它变成一个实例方法在你的输入循环中像这样调用它:

taxarray[x].getRates();

这将需要对您的getRates方法进行一些更改,特别是让它使用this引用而不是创建一个新的TaxPayer实例。