问题
Write a program that calculates and displays the take-home pay for a commissioned sales employee after deductions are taken. The employee receives 7% of his or her total sales as his or her gross pay. His or her federal tax rate is 18%. He or she contributes 10% to his or her retirement program and 6% to Social Security. Use the Processing Logic provided in Step 2 below as a guide. The program's output should look something like this:
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.
//Here is my Code, I am having trouble getting input
//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();
回答1:
Proper method:
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();
Easy method:
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.");
}
回答2:
weeklySales = Console.Read();
Is not going to do what you think it does. It will return the ASCII value of the first char.
Instead:
string weeklySalesText = Console.ReadLine();
weeklySales = int.Parse(weeklySalesText ); // may need more processing.
回答3:
You are trying to calculate the result using the variable before you put anything in the variable, that doesn't work. Do the input before you calculate the result.
回答4:
Console.Read() doesn't do what you think it does. It gets the first new character, and returns it as an int. It doesn't read the whole line. Also, the Console.ReadLine() call after that is completely meaningless, since you're not doing anything with the result.
I think you want the user to write a line, and then capture the contents of that line, check if it's and int, if it's not write an error message and try again, and if it is, do stuff with it.
This is done as such:
while (!int.TryParse(Console.ReadLine(), out weeklySales))
{
Console.WriteLine("Input was not a number.");
}
Replace
weeklySales = Console.Read();
System.Console.ReadLine();
With that, and afterwards, you can use weeklySales to calculate all other required variables.
Also, the definitions at the start shouldn't work either, as weeklySales isn't initialized at the start.
EDIT: As a little explanation how my method works: you are calling int.TryParse(string, out int), which returns false if it the given string is not a number, and else parses it to an int and returns true.
The thing you want to parse to an integer is the console input, therefore, the first parameter should be Console.ReadLine(). weeklySales is obviously the number you want to convert the string to, and is therefore provided as an out parameter (look that up if you don't know what that is).
If int.TryParse returns false (and has therefore failed), we run the while loop to inform the user, and ask again. If not, then we continue the program.
来源:https://stackoverflow.com/questions/14306562/reading-and-processing-input