Reputation: 57
Absolute newbie to C#. Was trying to run this program and the output simply would not show any computations.Why? I did not want to go through p,q,r,s for add, sub, multiply, divide etc., Also, how can i put space between "Please enter a number" and userName?
string userName;
double x, y;
Console.WriteLine(" Enter Your Name ");
userName = Console.ReadLine();
Console.WriteLine(" Please Enter A Number "+ userName);
First = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please Enter Another Number"+ userName);
Second = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The addition of Two Numbers is",x,y, x*y);
Console.WriteLine("The substraction of Two Numbers is",x,y,x/y);
Console.WriteLine("The multiplication of Two Numbers is",x,y,x * y);
Console.WriteLine("The division of Two Numbers given is", x,y,x / y);
Console.ReadKey();
Upvotes: 4
Views: 841
Reputation: 21
Totally agree with dasblinkenlight. Additionally, you may meet this line of code
Console.WriteLine("{0}, {1}", "Hello", 53);
Result in this line being written: Hello, 53. {0} is the placeholder for the first argument after the format string, {1} is the second, and so on. This is called composite formatting in .NET - http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.110%29.aspx
Upvotes: 0
Reputation: 726479
When you pass additional parameters to show output, you must tell WriteLine
where to put it by adding placeholders to the format line, like this:
Console.WriteLine("The product of Two Numbers {0} and {1} is {2}", x, y, x*y);
Positions are zero-based. The printed value of the first additional parameter (i.e. x
) will replace {0}
; the value of y
will replace {1}
, and the value of x*y
will replace {2}
in the final output.
The reason you did not have to do it with userName
is that you passed a single parameter:
Console.WriteLine("Please Enter Another Number " + userName);
The result of appending userName
to "Please Enter Another Number"
string is passed as a single parameter to WriteLine
. You could rewrite it with a format specifier, like this:
Console.WriteLine("Please Enter Another Number {0}", userName);
Upvotes: 11