简体   繁体   中英

working with console writeline c#

I need to output the value of d using the same Console.WriteLine . But i am only getting Result not the value of d in output. In what way i can achieve this?

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            Console.WriteLine("Please Enter the first Digit");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please Enter the Second Digit");
            b = Convert.ToInt32(Console.ReadLine());
            int d = a + b;
            Console.WriteLine("Result",(d));

        }
    }
}

Use:

Console.WriteLine("Result {0}", d);

You are using this overload .

UPDATE

If you look at the link above, you can read how it works. In short, first you specify the formatting, where {0} references the first value of the param object-array, {1} references the second value of the param object-array, etc. After the format, you give the objects to use.

So in your case, you need a single value, which means two arguments, a format, and a value. Hence "Result {0}" with d, which will become (when for example d=10) "Result 10".

Note: also removed the unnecessary parenthesis.

使用

Console.WriteLine("Result {0}", d);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM