编写一个程序,计算并显示扣除后的委托销售员工的实得工资。该员工将其总销售额的7%作为其总薪酬。他或她的联邦税率为18%。他或她为退休计划贡献10%,为社会保障贡献6%。使用下面步骤2中提供的处理逻辑作为指导。该程序的输出应如下所示:
Enter weekly sales: 28,000 Total sales: $28,000.00 Gross pay (7%): $1,960.00 Federal tax paid: $352.80 Social security paid: $117.60 Retirement contribution: $196.00 Total deductions: $666.40 Take home pay: $1,293.60 Press any key to continue.
//这是我的代码,我无法获得输入
//Declarations
int weeklySales;
double grossPay = weeklySales * .07;
double fedTax = grossPay * .18;
double retirement = grossPay * .1;
double socSecurity = grossPay * .06;
double totDeductions = socSecurity + retirement + fedTax;
double takeHomePay = grossPay - totDeductions;
//Promt user for Input
System.Console.WriteLine("What is your weekly Sales?");
weeklySales = Console.Read();
System.Console.ReadLine();
//Output Display
System.Console.Write("\nYour Weekly Sale amount is :\t\t" + weeklySales+ "\n\nGross Pay:\t\t\t\t" + grossPay+ "\n\nFed Tax \t\t\t\t" + fedTax + "\n\nRetirement\t\t\t\t"+ retirement + "\n\nSocial Security:\t\t\t" + socSecurity + "\n\nTotal Deductions:\t\t\t" + totDeductions + "\n\nMaking your take home pay:\t\t" + takeHomePay);
System.Console.ReadLine();
答案 0 :(得分:3)
weeklySales = Console.Read();
不会按照你的想法去做。它将返回第一个char的ASCII值。
相反:
string weeklySalesText = Console.ReadLine();
weeklySales = int.Parse(weeklySalesText ); // may need more processing.
答案 1 :(得分:0)
在将任何内容放入变量之前,您尝试使用变量计算结果,但这不起作用。在计算结果之前进行输入。
答案 2 :(得分:0)
Console.Read()
没有按照您的想法行事。它获取第一个新角色,并将其作为int
返回。它不读全行。此外,之后的Console.ReadLine()
调用完全没有意义,因为你没有对结果做任何事情。
我认为你希望用户写一行,然后捕获该行的内容,检查它是否和int
,如果它没有写错误信息再试一次,如果是,那么它的东西。
这样做是这样的:
while (!int.TryParse(Console.ReadLine(), out weeklySales))
{
Console.WriteLine("Input was not a number.");
}
替换
weeklySales = Console.Read();
System.Console.ReadLine();
然后,您可以使用weeklySales
来计算所有其他必需变量。
此外,开头的定义也不应该起作用,因为在开始时没有初始化weeklySales。
编辑:稍微解释一下我的方法是如何工作的:你正在调用int.TryParse(string, out int)
,如果给定的字符串不是数字,则返回false
,然后解析它到一个int并返回true
。
要解析为整数的是控制台输入,因此,第一个参数应为Console.ReadLine()
。 weeklySales
显然是您要将string
转换为的数字,因此提供为out
参数(如果您不知道那是什么,请查看)。
如果int.TryParse
返回false
(因此失败了),我们会运行while循环来通知用户,然后再次询问。如果没有,那么我们继续该计划。
答案 3 :(得分:0)
正确的方法:
Int32 weeklySales = 0;
while (weeklySales.Equals(0))
{
Console.WriteLine("What are your weekly sales?");
String input = Console.ReadLine();
try
{
weeklySales = Int32.Parse(input);
}
catch
{
Console.WriteLine("There is an error in your input, try again!");
}
}
Double grossPay = weeklySales * .07;
Double fedTax = grossPay * .18;
Double retirement = grossPay * .1;
Double socSecurity = grossPay * .06;
Double totDeductions = socSecurity + retirement + fedTax;
Double takeHomePay = grossPay - totDeductions;
Console.Write("\nYour Weekly Sale amount is :" + weeklySales + "$\nGross Pay: " + grossPay + "$\nFed Tax: " + fedTax + "$\nRetirement: " + retirement + "$\nSocial Security: " + socSecurity + "$\nTotal Deductions: " + totDeductions + "$\nMaking your take home pay: " + takeHomePay + "$");
Console.Read();
简单的方法:
Console.WriteLine("What are your weekly sales?");
try
{
Int32 weeklySales = Int32.Parse(Console.ReadLine());
Double grossPay = weeklySales * .07;
Double fedTax = grossPay * .18;
Double retirement = grossPay * .1;
Double socSecurity = grossPay * .06;
Double totDeductions = socSecurity + retirement + fedTax;
Double takeHomePay = grossPay - totDeductions;
Console.Write("\nYour Weekly Sale amount is :" + weeklySales + "$\nGross Pay: " + grossPay + "$\nFed Tax: " + fedTax + "$\nRetirement: " + retirement + "$\nSocial Security: " + socSecurity + "$\nTotal Deductions: " + totDeductions + "$\nMaking your take home pay: " + takeHomePay + "$");
Console.Read();
}
catch
{
Console.WriteLine("There is an error in input. Program will be terminated.");
}