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

用C#如何打印?
周六周日老师给我们出了10道题,还剩一道没有做出来,请大家指点一下,记住要用C#写奥~!谁有想法都可以写上,看看谁的方法最简单还能看明白,可以写点注释! 


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


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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;
string str = "";
Console.WriteLine ("请输入一个1-20内的数字");
num = Console.Read();
for (int i = 1; i <= num - 48; i++)// Console.Read()读取的是字符,字符以int型(32位整数)返回变量.0为48
{
str += i.ToString() + ",";
}
str = str.Substring(0,str.Length-1);
Console.WriteLine(str );
}
}
}