McJeremy&Fan 通过本文主要向大家介绍了c#for循环语句,c#中for语句的用法,c#中for语句,c#for语句,c#foreach语句等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了c#用for语句输出一个三角形的方法。分享给大家供大家参考。具体分析如下:
这是一道面试题,要求是这样的:
只使用一个for循环输出下面图形:

如果可以使用2个for(即嵌套循环的话),那这题就很简单了。
但只能用一个for,这可把我想得,想到面试都结束了没想出来。
后来使用String对象,可以达成输出重复字符的效果!!!
代码贴在下面:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Render(19);
Console.Read();
}
static void Render(int rowNum)
{
if (rowNum <= 0 || !System.Text.RegularExpressions.Regex.IsMatch(rowNum.ToString(),@"^\d+$"))
return;
int tmpRow = 0;
for (int i = 1; i <= rowNum; i++)
{
//对称输出
tmpRow = i <= rowNum / 2 ? i : rowNum - i + 1;
Console.WriteLine("{0}", new string('*', 2 * tmpRow - 1));
}
}
}
}
</div>
希望本文所述对大家的C#程序设计有所帮助。
</div>
