通过调用计算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 的相关文章

  • pthread_cond_timedwait() 和 pthread_cond_broadcast() 解释

    因此 我在堆栈溢出和其他资源上进行了大量搜索 但我无法理解有关上述函数的一些内容 具体来说 1 当pthread cond timedwait 因为定时器值用完而返回时 它如何自动重新获取互斥锁 互斥锁可能被锁定在其他地方 例如 在生产者
  • 每个术语出现的次数

    我得到了一个数组a n 2 where n can be 10 5最大时有n个科目和n个学生 全部编号为 1 2 n a i 0 and a i 1 1 lt i lt n 表示在第 i 个科目中 所有来自a i 0 to a i 1 通过
  • Newtonsoft JSON PreserveReferences处理自定义等于用法

    我目前在使用 Newtonsoft Json 时遇到一些问题 我想要的很简单 将要序列化的对象与所有属性和子属性进行比较以确保相等 我现在尝试创建自己的 EqualityComparer 但它仅与父对象的属性进行比较 另外 我尝试编写自己的
  • WPF 中的调度程序和异步等待

    我正在尝试学习 WPF C 中的异步编程 但我陷入了异步编程和使用调度程序的困境 它们是不同的还是在相同的场景中使用 我愿意简短地回答这个问题 以免含糊不清 因为我知道我混淆了 WPF 中的概念和函数 但还不足以在功能上正确使用它 我在这里
  • 将目录压缩为单个文件的方法有哪些

    不知道怎么问 所以我会解释一下情况 我需要存储一些压缩文件 最初的想法是创建一个文件夹并存储所需数量的压缩文件 并创建一个文件来保存有关每个压缩文件的数据 但是 我不被允许创建许多文件 只能有一个 我决定创建一个压缩文件 其中包含有关进一步
  • C 预处理器库

    我的任务是开发源分析工具C程序 并且我需要在分析本身之前预处理代码 我想知道什么是最好的图书馆 我需要一些重量轻 便于携带的东西 与其推出自己的 为什么不使用cpp这是的一部分gcc suite http gcc gnu org onlin
  • 如果使用 SingleOrDefault() 并在数字列表中搜索不在列表中的数字,如何返回 null?

    使用查询正数列表时SingleOrDefault 当在列表中找不到数字时 如何返回 null 或像 1 这样的自定义值 而不是类型的默认值 在本例中为 0 你可以使用 var first theIntegers Cast
  • Numpy - 根据表示一维的坐标向量的条件替换数组中的值

    我有一个data多维数组 最后一个是距离 另一方面 我有距离向量r 例如 Data np ones 20 30 100 r np linspace 10 50 100 最后 我还有一个临界距离值列表 称为r0 使得 r0 shape Dat
  • 在 ASP.NET Core 3.1 中使用包含“System.Web.HttpContext”的旧项目

    我们有一些用 Net Framework编写的遗留项目 应该由由ASP NET Core3 1编写的API项目使用 问题是这些遗留项目正在使用 System Web HttpContext 您知道它不再存在于 net core 中 现在我们
  • 如何将图像路径保存到Live Tile的WP8本地文件夹

    我正在更新我的 Windows Phone 应用程序以使用新的 WP8 文件存储 API 本地文件夹 而不是 WP7 API 隔离存储文件 旧的工作方法 这是我如何成功地将图像保存到 共享 ShellContent文件夹使用隔离存储文件方法
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • 在数据库中搜索时忽略空文本框

    此代码能够搜索数据并将其加载到DataGridView基于搜索表单文本框中提供的值 如果我将任何文本框留空 则不会有搜索结果 因为 SQL 查询是用 AND 组合的 如何在搜索 从 SQL 查询或 C 代码 时忽略空文本框 private
  • 从库中捕获主线程 SynchronizationContext 或 Dispatcher

    我有一个 C 库 希望能够将工作发送 发布到 主 ui 线程 如果存在 该库可供以下人员使用 一个winforms应用程序 本机应用程序 带 UI 控制台应用程序 没有 UI 在库中 我想在初始化期间捕获一些东西 Synchronizati
  • C++ 复制初始化和直接初始化,奇怪的情况

    在继续阅读本文之前 请阅读在 C 中 复制初始化和直接初始化之间有区别吗 https stackoverflow com questions 1051379 is there a difference in c between copy i
  • 为什么 C# Math.Ceiling 向下舍入?

    我今天过得很艰难 但有些事情不太对劲 在我的 C 代码中 我有这样的内容 Math Ceiling decimal this TotalRecordCount this PageSize Where int TotalRecordCount
  • Process.Start 阻塞

    我正在调用 Process Start 但它会阻止当前线程 pInfo new ProcessStartInfo C Windows notepad exe Start process mProcess new Process mProce
  • ASP.NET MVC 6 (ASP.NET 5) 中的 Application_PreSendRequestHeaders 和 Application_BeginRequest

    如何在 ASP NET 5 MVC6 中使用这些方法 在 MVC5 中 我在 Global asax 中使用了它 现在呢 也许是入门班 protected void Application PreSendRequestHeaders obj
  • 防止索引超出范围错误

    我想编写对某些条件的检查 而不必使用 try catch 并且我想避免出现 Index Out of Range 错误的可能性 if array Element 0 Object Length gt 0 array Element 1 Ob
  • 使用随机放置的 NaN 创建示例 numpy 数组

    出于测试目的 我想创建一个M by Nnumpy 数组与c随机放置的 NaN import numpy as np M 10 N 5 c 15 A np random randn M N A mask np nan 我在创建时遇到问题mas
  • 使用按位运算符相乘

    我想知道如何使用按位运算符将一系列二进制位相乘 但是 我有兴趣这样做来查找二进制值的十进制小数值 这是我正在尝试做的一个例子 假设 1010010 我想使用每个单独的位 以便将其计算为 1 2 1 0 2 2 1 2 3 0 2 4 虽然我

随机推荐