c#使用运算符计算(净工资)(总工资)

时间:2009-03-17 03:24:40

标签: c#

我不明白netPay = grossPay - (fedtax withholding + social security tax withholding)的计算方法。我的计算在程序中是否正确?处理editedTax ??如果有人可以提供帮助,我会很感激。

更多信息:当我在输出中显示netPay时,我收到运行时错误,我无法使用{0:c2}将负数转换为货币。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string empName;
            string userInput;

            double netPay;
            double editedTax1;
            double grossPay;
            double editedTax2;
            double hrsWorked;
            double ovtWorked;
            double payRate;

            const double FED_TAX = .28;
            const double SS_TAX = 7.65;




            // step 1
            Console.WriteLine("       WEEKLY PAYROLL INFORMATION");

            // step 2
            Console.WriteLine("       --------------------------");

            // step 3
            Console.Write("\n       Please enter the employer's name: ");
            empName = Console.ReadLine();

            //step 4
            Console.Write("\n       Please enter the number of hours worked this week: ");
            userInput = Console.ReadLine();
            hrsWorked = Convert.ToDouble(userInput);

            // step 5
            Console.Write("\n       Please enter the number of OVERTIME HOURS worked this week: ");
            userInput = Console.ReadLine();
            ovtWorked = Convert.ToInt32(userInput);

            // step 6
            Console.Write("\n       Please enter employee's HOURLY PAY RATE: ");
            userInput = Console.ReadLine();
            payRate = Convert.ToDouble(userInput);

            // step 7
            grossPay = (hrsWorked * payRate + ovtWorked * 1.5 * payRate);

            // step 8
            editedTax1 = FED_TAX * grossPay;

            // step 9
            editedTax2 = SS_TAX * grossPay;

            // step 10
            netPay = editedTax1 + editedTax2 - grossPay;

            // step 11
            Console.WriteLine("\n\n       The weekly payroll information summary for: " + empName);

            Console.WriteLine("\n       Gross pay:                             {0:C2}    ", grossPay);

            // step 12
            Console.WriteLine("       Federal income taxes witheld:          {0:C2}      ", editedTax1);
            Console.WriteLine("       Social Security taxes witheld:         {0:C2}    ", editedTax2);
            Console.WriteLine("       Net Pay:                               {0:C2}", netPay);


        }
    }
}

4 个答案:

答案 0 :(得分:2)

与您在下面的说明相比,

netPay在代码中分配了相反的值 我没有看到任何语法错误或其他任何内容。

你遇到的问题是什么?您尝试过的一些事情是什么?

答案 1 :(得分:2)

您在计算中得到负数的原因是因为您的SS_TAX是7.65。我想你想要的数字是0.0765。

答案 2 :(得分:0)

我不确定你要求的是什么,但是赋值语句中的简单替换会产生以下公式:

由于

editedTax1 = FED_TAX * grossPay;

editedTax2 = SS_TAX * grossPay;

netPay = editedTax1 + editedTax2 - grossPay;

然后

netPay = FED_TAX * grossPay + SS_TAX * grossPay - grossPay;

含义

netPay = grossPay * (FED_TAX + SS_TAX - 1);

所以这里的东西似乎有点......


您确定不想要

吗?
netPay = grossPay - (editedTax1 + editedTax2);

而不是

netPay = editedTax1 + editedTax2 - grossPay;

这似乎符合您的要求

netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);

netPay = grossPay * (1 - (FED_TAX + SS_TAX));

......除非我遗漏了某些东西,当然。


编辑:我错过了一些东西。您的税收常数是百分比,但是当您使用它们进行计算时,您不会除以100。您有两种选择:

  1. 在计算中使用值时除以100,如:

    editedTax1 = (FED_TAX / 100) * grossPay;

  2. 将常量存储为十进制表示,而不是百分比,如:

    const double FED_TAX = .0028;

答案 3 :(得分:0)

我已经在我的机器上测试了这段代码,它运行正常:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            const double FEDERAL_TAX_RATE= 0.28;
            const double SOCIAL_SECURITY_RATE = 0.0765;  // I am assuming the 7.65 was supposed to be 7.65%... therefore it should be 0.0765

            Console.WriteLine("       WEEKLY PAYROLL INFORMATION");
            Console.WriteLine("       --------------------------");
            Console.Write("\n       Please enter the employer's name: ");
            string empName = Console.ReadLine();
            Console.Write("\n       Please enter the number of hours worked this week: ");
            double hrsWorked = Convert.ToDouble(Console.ReadLine());
            Console.Write("\n       Please enter the number of OVERTIME HOURS worked this week: ");
            double ovtWorked = Convert.ToInt32(Console.ReadLine());
            Console.Write("\n       Please enter employee's HOURLY PAY RATE: ");
            double payRate = Convert.ToDouble(Console.ReadLine());
            double grossPay = CalculateGrossPay(hrsWorked, payRate, ovtWorked);
            double federalTaxWithheld = CalculateTax(grossPay, FEDERAL_TAX_RATE);
            double socialSecurityWithheld = CalculateTax(grossPay, SOCIAL_SECURITY_RATE);
            double netPay = CalculateNetPay(grossPay, federalTaxWithheld + socialSecurityWithheld);

            Console.WriteLine("\n\n       The weekly payroll information summary for: " + empName);
            Console.WriteLine("\n       Gross pay:                             {0:C2}    ", grossPay);
            Console.WriteLine("       Federal income taxes witheld:          {0:C2}      ", federalTaxWithheld);
            Console.WriteLine("       Social Security taxes witheld:         {0:C2}    ", socialSecurityWithheld);
            Console.WriteLine("       Net Pay:                               {0:C2}", netPay);

            Console.ReadLine();  // You don't need this line if your running from the command line to begin with
        }

        static double CalculateGrossPay(double HoursWorked, double PayRate, double OvertimeHoursWorked)
        {
            return PayRate * (HoursWorked + 1.5 * OvertimeHoursWorked);
        }

        static double CalculateTax(double GrossPay, double TaxRate) 
        {
            return GrossPay * TaxRate;
        }

        static double CalculateNetPay(double GrossPay, double TaxAmount)
        {
            return GrossPay - TaxAmount;
        }
    }
}

因为看起来这是你的第一个编程课程,我会给你一些他们可能不会在你班上强调的指示:

  • 使用描述性变量名称。如果您发现自己在变量名后面添加了一个数字,那么它可能会以不同且更易读的方式完成!
  • 利用功能,它们将常见任务捆绑在一段代码中,并显着提高可读性。
  • 您可能需要为此代码添加一些exception handling或验证。例如,如果您不小心将-1传递到OverTimeHours
  • ,该怎么办?

我知道在你的编程过程中这可能并不重要,但是开始使用编码技术总是好的,这些编码技术会使你的代码更具可读性并且更容易混淆,特别是对于那些将来可能需要维护它的人来说。