c#使用操作员进行计算(净薪酬)(总薪酬)
我不明白是否计算了c#使用操作员进行计算(净薪酬)(总薪酬),c#,C#,我不明白是否计算了netPay=grossPay-(fedtax预扣税+社会保障税预扣税)。我的计算在程序中正确吗?在editedTax中处理此类问题??如果有人能帮忙,我会很感激的 更多信息:当我在输出中显示netPay时,我收到一个运行时错误,无法使用{0:c2}将负数转换为货币 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication5 { cla
netPay=grossPay-(fedtax预扣税+社会保障税预扣税)
。我的计算在程序中正确吗?在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);
}
}
}
netPay
在代码中分配的值与您下面的描述相反。我没有看到任何语法错误或任何东西
你有什么问题?您尝试过的一些方法是什么?我不确定您到底想要什么,但赋值语句中的简单替换会产生以下公式: 自 然后 意义
netPay = grossPay * (FED_TAX + SS_TAX - 1);
所以这里似乎有点不对劲
你确定你不想要吗
netPay = grossPay - (editedTax1 + editedTax2);
而不是
netPay = editedTax1 + editedTax2 - grossPay;
这似乎符合你的要求
netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);
或
…当然,除非我遗漏了什么
编辑:我遗漏了一些东西。你的税收常数是百分之一,但当你用它们进行计算时,你不能除以100。您有两个选择:
editedTax1=(联邦税/100)*grossPay代码>
const double FED_TAX=.0028代码>
你在计算中得到负数的原因是因为你的SSU税是7.65。我想你想要的数字是0.0765。我已经在我的机器上测试了这段代码,它工作正常:
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;
}
}
}
由于这似乎是您的第一门编程课程,我将为您提供一些他们在课堂上可能不会强调的要点:
- 使用描述性变量名。如果您注意到自己在变量名后面加了一个数字,它可能会以一种不同的、更可读的方式完成李>
- 利用函数,它们将常见任务捆绑在一段代码中,并显著提高可读性
- 您可能希望在此代码中添加一些或验证。例如,如果您意外地将
传递到-1
,该怎么办超时时间
我知道这在您的编程过程中可能并不重要,但开始时最好使用编码技术,使您的代码更易读、更容易混淆,特别是对于将来可能需要维护代码的人。您可能希望将editedTax1和editedTax2重命名为federalTax和socialSecurityTax,或者类似的东西。这将使代码更容易阅读。我很好奇为什么这个问题被否决。请解释一下?因为问题没有澄清,只是“这是我的公式,但我懒得把它复制到程序中”@Brent,我试图产生你提到的运行时错误。您在程序中使用了哪些输入?我想要第一个输入,netPay=grossPay-(editedTax1+editedTax2);但是当我在输出中显示它时,我发现了一个运行时错误,我不能用{0:c2}将负数转换为货币。这两个语句应该给出不同的答案,对吗?特别是在不同的输入上?这两个语句正好相反,除非grossPay为零,否则会给出不同的答案。这与3-2=1和2-3=-1是一样的。其次,我再次查看了您的代码,找到了负数的原因。答案已编辑。我希望在标记为“家庭作业”的帖子上有一个否决票选项。但是你应该赢得家庭作业救世主的徽章,吉姆。很抱歉,如果我没有其他选择来提高我的编程知识的话。@Philoushka:我们都遇到过这样的情况,我们离问题太近了,看不到明显的问题,需要一些帮助。这不像是他让我们为他做任务。我们在我工作的地方遇到过很多次这个问题。。。如果联邦税率为28%,则正确为0.28%。那么,为什么社会保障税是765%?如果是7.65%,那么你想要的数字实际上是0.0765。
netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);
netPay = grossPay * (1 - (FED_TAX + SS_TAX));
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;
}
}
}