您的位置:首页 > 编程语言 > C#

C# 一个例子,北大青鸟的。自己变了下。

2010-10-19 00:01 337 查看
using System;

using System.Collections.Generic;

using System.Text;

namespace PrintNumbers

{

///<summary>

///画一个松数.

///</summary>

///

public class DrawTree

{

int Width;

public int Width1

{

get { return Width; }

set { Width = value; }

}

int Height;

public int Height1

{

get { return Height; }

set { Height = value; }

}

int x, y;

public DrawTree()

{

Console.WriteLine("画一个松数,让你们见识我的强大吧。");

}

public void EnterWidth()

{

do

{

int temp = 0;

Console.Write("请输入松树的宽度:");

if (!int.TryParse(Console.ReadLine(), out temp))

{

Console.WriteLine("请输入数字!(奇数)");

continue;

}

else if (temp % 2 == 0)

{

Console.WriteLine("请输入数字,奇数!");

continue;

}

else

{

Width = temp;

break;

}

} while (true);

}

public void EnterHeight()

{

int temp = 0;

do

{

Console.Write("请输入松树的高度:");

if (!int.TryParse(Console.ReadLine(), out temp))

{

Console.WriteLine("请输入数字!");

continue;

}

else

{

Height = temp;

break;

}

} while (true);

}

public override string ToString()

{

return string.Format("\n\n\n\n=======================================\nNow ! 睁大你的狗眼吧,看看我将会为你画一个树子出来哦!\n树子度高是{0,-6}高度是{1,-5}", Width, Height);

}

public void DrawATree()

{

this.DrawATree(Width, Height);

}

public void DrawATree(int width, int height)

{

///<summary>

///画树冠

///</summary>

///

//for (int i = 0; i < length; i++)

//{

//}

for (int i = 1; i < width; i++)

{

for (int k = 0; k < width-i; k++)

{

Console.Write(" ");

}

for (int j = 0; j < 2*i-1; j++)

{

Console.Write("*");

}

Console.WriteLine();

}

for (int m = 0; m < height; m++)

{

for (int i = 0; i <width-1; i++)

{

Console.Write(" ");

}

Console.WriteLine("*\n");

}

}

}

class Program

{

static void Main(string[] args)

{

DrawTree tree = new DrawTree();

tree.EnterWidth();

tree.EnterHeight();

Console.WriteLine(tree.ToString());

tree.DrawATree();

}

}

}

//画一个松树,让你们见识我的强大吧。

请输入松树的宽度:12

请输入数字,奇数!

请输入松树的宽度:12

请输入数字,奇数!

请输入松树的宽度:15

请输入松树的高度:12

=======================================

Now ! 睁大你的狗眼吧,看看我将会为你画一个树子出来哦!

树子度高是15 高度是12

*

***

*****

*******

*********

***********

*************

***************

*****************

*******************

*********************

***********************

*************************

***************************

*

*

*

*

*

*

*

*

*

*

*

*

请按任意键继续. . .

//非常遗憾.控制台下面都是OK的,估计空格不一样吧。

////另外一个例子,找奇数的。

using System;

using System.Collections.Generic;

using System.Text;

namespace Sum

{

/// <summary>

/// 本示例为第二章上机课第一个作业题的参考答案

/// 求1-100奇数数字之和

/// </summary>

class Number

{

int x;

int y;

int temp;

public Number()

{

Console.WriteLine("求两个数之间的所有奇数");

}

///<summary>

///输入第一个数

///</summary>

///

public void EnterFirstNumber()

{

Console.WriteLine("请输入第一个数");

while(true)

{

if (int.TryParse(Console.ReadLine(),out temp))

{

x=temp;

break;

}

else

{

Console.WriteLine("请输入正确的数字");

continue;

}

}

}

///<summary>

///输入第二个数

///</summary>

///

public void SecondNumber()

{

Console.WriteLine("请输入第二个数");

while (true)

{

if (int.TryParse(Console.ReadLine(), out temp))

{

if (temp != x)

{

y = temp;

break;

}

else

{

Console.WriteLine("两个数字不能够相同");

continue;

}

}

else

{

Console.WriteLine("请输入正确的数字");

continue;

}

}

}

///<summary>

///找出奇数!

///</summary>

///

public void Find奇数()

{

int temp;

if (x > y)

{

temp = y;

y = x;

x = temp;

}

for ( ; x <= y; x++)

{

if (x%2!=0)

{

continue;

}

Console.Write("{0,-5}",x);

}

}

}

class Program

{

static void Main(string[] args)

{

Number number = new Number();

number.EnterFirstNumber();

number.SecondNumber();

number.Find奇数();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: