Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Writeline -- Why output is missing

Tags:

c#

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();
like image 319
Crazy Duke Avatar asked Dec 20 '22 08:12

Crazy Duke


1 Answers

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);
like image 148
Sergey Kalinichenko Avatar answered Jan 04 '23 00:01

Sergey Kalinichenko