日期:2014-05-18  浏览次数:20864 次

C# ReadLine(),WriteLine()问题
namespace ConsoleApplication2
{
  class Program
  {
  static void Main(string[] args)
  {
  Console.WriteLine("input:");
  //string x = Console.ReadLine();
  int x, y;
  x = Convert.ToInt32(Console.ReadLine());
  y = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("{0} + {0} = {0}", x, y, x + y);
  Console.ReadKey();
  }
  }
}

c#函数,输入两个数字x,y,得出它们的和.但是以上的代码不行,该怎么改?

------解决方案--------------------
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);

------解决方案--------------------
C# code
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);

------解决方案--------------------
Console.WriteLine("{0} + {0} = {0}", x, y, x + y);
 这是什么玩意?
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
------解决方案--------------------

C# code

  static void Main(string[] args)
            {
                int x, y;
                Console.WriteLine("input:");         
                x = Convert.ToInt32(Console.ReadLine());
                y = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("{0} + {1} = {2}", x, y, x + y);

                Console.ReadLine();

            }

------解决方案--------------------

你的占位符写错了。。。
------解决方案--------------------
Console.WriteLine("{0} + {0} = {0}", x, y, x + y);
这一句 有问题...
------解决方案--------------------

Console.WriteLine("{0} + {1} = {2}", x, y, x + y);

Console.WriteLine第一个参数stringformat中{0}对应后面的第一个参数(就是x),{1}对应着后面第二个参数(就是y),以此类推。。。

所以你全部写的{0},最后输出的是个1+1=1