用C#写一段代码,最终输出以下三角形 *
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
解决方案 »
- 悲剧,程序动不得
- TreeView 结点 ID 问题
- 求急: 怎样才能动态添加内容呢?
- C#中数据库类的设计
- 用FileInfo类的Open方法打开指定文件时,为什么不能像双击或者右键点击该文件选“打开”菜单项那样真正地打开文件的界面?怎样才能打开其
- 为什么float是32位的,但表示的数值范围比64位的long还大?
- 向SQL server数据库添加图片问题
- Visula Studio.net 2003 安装问题
- 如果是做数据库方面的项目,C#的效果如何?
- 谁有介绍如何使用unsafe code,API的资料,最全面的,看完就能干活的资料。
- 一个排序,用代码实现
- [清楚了,立即结贴]请问FontSize="24pt"为什么等于FontSize=32啊?
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
while (true)
{
string numstr = Console.ReadLine();
try
{
int num = Int32.Parse(numstr);
Console.Write(GetPrintString(num));
}
catch {
break;
}
}
}
static string GetPrintString(int a) {
if (a <= 0) { return ""; }
if (a == 1) { return "*"; }
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a; i++)
{
string left = "";//左边的空格
for (int j = i; j < a - 1; j++)
{
left += " ";
}
if (i == 0)
{
sb.AppendFormat("{0}*\r\n", left);
continue;
}
string middle = "";
middle += i == a - 1 ? "*" : " ";
for (int j = 0; j < i; j++)
{
if (j > 0) middle += i == a - 1 ? "**" : " ";
}
sb.AppendFormat("{0}*{1}*\r\n", left, middle);
}
return sb.ToString();
}
}
}
using System.Text;namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int i,j,k;
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
{
System.Console.WriteLine(" ");
}
for(k=1;k<=2*i-1;k++)
{
System.Console.WriteLine("*");
}
System.Console.WriteLine("\n");
}
}
}
}