通过调用计算taxAmount的方法只能得到$0

2024-02-22

我在执行以下调用时遇到问题,特别是最后一个组件:

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

我正在调用在 main 中作为taxRates 启动的Rates 类中的CalculateTax 方法。

这是CalculateTax方法

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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过调用计算taxAmount的方法只能得到$0 的相关文章

  • “构建”构建我的项目,“构建解决方案”则不构建

    我刚刚开始使用VS2010 我有一个较大的解决方案 已从 VS2008 成功迁移 我已将一个名为 Test 的控制台应用程序项目添加到解决方案中 选择构建 gt 构建解决方案不编译新项目 选择构建 gt 构建测试确实构建了项目 在失败的情况
  • GLKit的GLKMatrix“列专业”如何?

    前提A 当谈论线性存储器中的 列主 矩阵时 列被一个接一个地指定 使得存储器中的前 4 个条目对应于矩阵中的第一列 另一方面 行主 矩阵被理解为依次指定行 以便内存中的前 4 个条目指定矩阵的第一行 A GLKMatrix4看起来像这样 u
  • 动态加载程序集的应用程序配置

    我正在尝试将模块动态加载到我的应用程序中 但我想为每个模块指定单独的 app config 文件 假设我的主应用程序有以下 app config 设置
  • 查找c中结构元素的偏移量

    struct a struct b int i float j x struct c int k float l y z 谁能解释一下如何找到偏移量int k这样我们就可以找到地址int i Use offsetof 找到从开始处的偏移量z
  • 类模板参数推导 - clang 和 gcc 不同

    下面的代码使用 gcc 编译 但不使用 clang 编译 https godbolt org z ttqGuL template
  • 从Web API同步调用外部api

    我需要从我的 Web API 2 控制器调用外部 api 类似于此处的要求 使用 HttpClient 从 Web API 操作调用外部 HTTP 服务 https stackoverflow com questions 13222998
  • BitTorrent 追踪器宣布问题

    我花了一点业余时间编写 BitTorrent 客户端 主要是出于好奇 但部分是出于提高我的 C 技能的愿望 我一直在使用理论维基 http wiki theory org BitTorrentSpecification作为我的向导 我已经建
  • 如何从 appsettings.json 文件中的对象数组读取值

    我的 appsettings json 文件 StudentBirthdays Anne 01 11 2000 Peter 29 07 2001 Jane 15 10 2001 John Not Mentioned 我有一个单独的配置类 p
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • while 循环中的 scanf

    在这段代码中 scanf只工作一次 我究竟做错了什么 include
  • SolrNet连接说明

    为什么 SolrNet 连接的容器保持静态 这是一个非常大的错误 因为当我们在应用程序中向应用程序发送异步请求时 SolrNet 会表现异常 在 SolrNet 中如何避免这个问题 class P static void M string
  • 控件的命名约定[重复]

    这个问题在这里已经有答案了 Microsoft 在其网站上提供了命名指南 here http msdn microsoft com en us library xzf533w0 VS 71 aspx 我还有 框架设计指南 一书 我找不到有关
  • 如何序列化/反序列化自定义数据集

    我有一个 winforms 应用程序 它使用强类型的自定义数据集来保存数据进行处理 它由数据库中的数据填充 我有一个用户控件 它接受任何自定义数据集并在数据网格中显示内容 这用于测试和调试 为了使控件可重用 我将自定义数据集视为普通的 Sy
  • 使用日期 Swift 3 对字典数组进行排序

    我有一个名为 myArray 的数组 其中添加了字典 我希望该字典按时间排序 这是字典中的键 那个时间是在 String 中 时间的日期格式为 yyyy MM dd HH mm ss 我尝试使用下面的代码解决方案 但给出了 从 字符串转换
  • 为什么编译时浮点计算可能不会得到与运行时计算相同的结果?

    In the speaker mentioned Compile time floating point calculations might not have the same results as runtime calculation
  • 将控制台重定向到 .NET 程序中的字符串

    如何重定向写入控制台的任何内容以写入字符串 对于您自己的流程 Console SetOut http msdn microsoft com en us library system console setout aspx并将其重定向到构建在
  • 是否可以在 .NET Core 中将 gRPC 与 HTTP/1.1 结合使用?

    我有两个网络服务 gRPC 客户端和 gRPC 服务器 服务器是用 NET Core编写的 然而 客户端是托管在 IIS 8 5 上的 NET Framework 4 7 2 Web 应用程序 所以它只支持HTTP 1 1 https le
  • 哪种 C 数据类型可以表示 40 位二进制数?

    我需要表示一个40位的二进制数 应该使用哪种 C 数据类型来处理这个问题 如果您使用的是 C99 或 C11 兼容编译器 则使用int least64 t以获得最大的兼容性 或者 如果您想要无符号类型 uint least64 t 这些都定
  • 如何在文本框中插入图像

    有没有办法在文本框中插入图像 我正在开发一个聊天应用程序 我想用图标图像更改值 等 但我找不到如何在文本框中插入图像 Thanks 如果您使用 RichTextBox 进行聊天 请查看Paste http msdn microsoft co
  • C++ 标准是否指定了编译器的 STL 实现细节?

    在写答案时this https stackoverflow com questions 30909296 can you put a pimpl class inside a vector我遇到了一个有趣的情况 这个问题演示了这样一种情况

随机推荐