471,350 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Simple math program.

Would someone be kind and explain why and how this works. It comes up with the correct answer, but I dont understand how it does. Wouldn't Console.WriteLine("5+4={0}",5+4); wouldn't it come up as 5+4=1 instead of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for the help in advance!
using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;
//addition with integers works as expected
Console.WriteLine("5 + 4 = {0}", 5 + 4);
Console.Write("Please press \"enter\" to continue");

Console.ReadLine();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson
Dec 5 '05 #1
3 1370
Eric Anderson wrote:
Would someone be kind and explain why and how this works. It comes up with the
correct answer, but I dont understand how it does. Wouldn't
Console.WriteLine("5+4={0}",5+4); wouldn't it come up as 5+4=1 instead of 5+4=9?
Sorry guys im new to all of this but im learning. Thanks for the help in advance!


Why would it come up as 5+4=1?
If you wrote: Console.WriteLine ("5+4={0}", a); that would come up as
5+4=1, but you're passing it the right answer, so I don't see why you'd
expect it to print the wrong one...

Jon

Dec 5 '05 #2
The {0} is substituted for the appropriate argument (in this case 5+4).
The argument evaluates to 9 and is inserted into the string as position
{0}. Why did you expect it to be 1? Not having a go, just interested.

Simon

As a further example

Console.WriteLine("5 + 4 = {0} and 5 - 4 = {1}", 5+4, 5-4);

would result in

5 + 4 = 9 and 5 - 4 = 1

Eric Anderson wrote:
Would someone be kind and explain why and how this works. It comes up
with the correct answer, but I dont understand how it does. Wouldn't
Console.WriteLine("5+4={0}",5+4); wouldn't it come up as 5+4=1 instead
of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for
the help in advance!

using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;

//addition with integers works as expected

Console.WriteLine("5 + 4 = {0}", 5 + 4);

Console.Write("Please press \"enter\" to continue");

Console.ReadLine();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson

Dec 5 '05 #3
Lol I know that 5+4 equals to. The reason why I asked is just to understand what was going on in that line of code, because the book that I am reading didn't clarify what all that meant. And I just want to get a full understanding of everything before I move on to the next part. But thanks guys for clearing that up for me.

--
(\(\
(=':')
(,(")(") Eric Anderson
"Eric Anderson" <ti*********@email.uophx.edu> wrote in message news:eF****************@tk2msftngp13.phx.gbl...
Would someone be kind and explain why and how this works. It comes up with the correct answer, but I dont understand how it does. Wouldn't Console.WriteLine("5+4={0}",5+4); wouldn't it come up as 5+4=1 instead of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for the help in advance!
using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;
//addition with integers works as expected
Console.WriteLine("5 + 4 = {0}", 5 + 4);
Console.Write("Please press \"enter\" to continue");

Console.ReadLine();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson
Dec 5 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by drs | last post: by
reply views Thread by Jussi Mononen | last post: by
17 posts views Thread by cwdjrxyz | last post: by
32 posts views Thread by someone else | last post: by
7 posts views Thread by Wernfried Schwenkner | last post: by
5 posts views Thread by aguirre.adolfo | last post: by
7 posts views Thread by bvdp | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.