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

大家帮帮忙。。。急用~~
要求:用C#写!!
试题一 

  创建一个控制台应用程序, 接受用户分别输入的1个整数,该整数范围是1-10, 
  当X=2时,打印 
  1 
  2 3 
  当X=5时,打印 
  1 
  2 3 
  4 5 6 
  7 8 9 10 
  11 12 13 14 15 
  以此类推

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x;
x=Console.Read();
try
{
x = Convert.ToInt32(((char)x).ToString());
}
catch 
{
x = 0;
}

if (x < 1 || x > 10)
{
Console.Write("invalid argument");
Console.Read();
Console.Read();
Console.Read();
return;
}

int val=1; //要打印的数
int line = 0; //控制行

for (line = 1; line <= x; line++)
{
//第i 行有i 个数
for (int i = 0; i < line; i++)
{
Console.Write(val);
Console.Write(" ");
val++;
}
Console.Write("\n");
}

Console.Read();
Console.Read();
Console.Read();


}
}
}