I want to print the value of the amount of sales开发者_StackOverflow社区 entered by user times .10. My first issue is I want to store a value that a user enters into sales then times that by .10 then print the value of sales. We I run the program I get two lines one is the amount of sales entered the other is .5.
const double COMMRATE = 0.10;
string inputstring;
double sales =5;
char response;
Console.Write("Do you want to Calculate Sales A or B or E...");
inputstring = Console.ReadLine();
response = Convert.ToChar(inputstring);
while (response == 'A')
{
Console.WriteLine("Enter Sales", sales.ToString("C"));
sales = sales * COMMRATE;
inputstring = Console.ReadLine();
response = Convert.ToChar(inputstring);
}
Your .5 is coming about because you've set the default value of sales to be 5, and 5 * 0.1 = 0.5.
But why is that happening? Let's take apart your loop:
- It asks the user to enter an amount of sales. The second parameter is ignored, because "Enter Sales" has no format string placeholders. It does not perform line-reading of any form.
- Without asking for input, it multiplies Sales by the commission rate and stores that to Sales.
- Now it asks for user input,
- which it immediately spits back out exactly as written (the amount-of-sales-entered line.)
What you actually need to be doing is a second Console.ReadLine() to get a string for the amount of sales, then use Double.ParseDouble() to get the entered amount of sales. Multiply that by your COMMRATE and print that back out- then give your "Do you want to Calculate..." question again, and then use the response from that to decide whether or not to continue the loop- currently, response is never getting modified, so you've created an infinite loop.
Unfortunately, you aren't even really close to the right code here. Your loop should look more like
while (response == 'A'){
Console.WriteLine("Enter Sales");
string salesStr = Console.ReadLine();
Console.WriteLine(Double.Parse(salesStr) * COMMRATE);
Console.WriteLine("Enter A to continue, anything else to quit");
response = Convert.ToChar(Console.ReadLine());
}
...which should get you started towards making your program do what you want.
Is this what you intend?
const double COMMRATE = 0.10;
string inputstring;
double sales;
char response;
Console.Write("Do you want to Calculate Sales A or B or E...");
inputstring = Console.ReadLine();
response = Convert.ToChar(inputstring);
while (response == 'A')
{
Console.WriteLine("Enter Sales");
inputstring = Console.ReadLine();
sales = Double.Parse(inputstring);
Console.WriteLine("Sales = " & sales);
Console.WriteLine("Commission = " & sales * COMMRATE);
}
I'm having a hard time discerning what exactly you're asking.
If you want to print the value multiplied by the COMMRATE then you should probably put the calculation above the write statement.
sales = sales * COMMRATE;
Console.WriteLine("Enter Sales", sales.ToString("C"));
Although since you said you want to STORE the value entered by the user and THEN print it times the COMMRATE maybe you should use something like
double calculatedSales = sales * COMMRATE
Console.WriteLine("Enter Sales", calculatedSales.ToString("C"));
so that you're not redefining the original sales amount
精彩评论