1,switch语句:判断用户输入的月份属于什么季节.
namespace switch语句
{
class Program
{
static void Main(string[] args)
{
//判断用户输入的月份所在的季节
Console.WriteLine("请您输入一个月份!");
int MyMouth = int.Parse(Console.ReadLine()); //变量MyMouth用于获取用户输入的数据
string MySeason;
switch (MyMouth)
{
case 12:
case 1:
case 2:
MySeason ="此月份是冬季!学敏记得保暖哦!";
break; //跳出switch语句
case 3:
case 4:
case 5:
MySeason ="此月份是春季!学敏记得戴眼镜,避大风哦!";
break;
case 6:
case 7:
case 8:
MySeason ="此月份是夏季!学敏记得吃雪糕,穿热裤哦!";
break;
case 9:
case 10:
case 11:
MySeason ="此月份是秋季!学敏这是个丰收的季节,给家里常打电话问候哦!";
break;
default :
MySeason = "月份输入错误!学敏温馨提示:只存在1~12月哦,亲!";
break;
}
Console .WriteLine (MySeason );
Console .ReadLine ();
}
}
}
运行结果:
2,while语句:声明两个int型变量 为s、num,初始值分别为0和100. 通过while语句循环输出。当s>50时,使用break语句终止循环;当s为偶数时,使用continue开始下一个循环。
namespace while语句
{
class Program
{
static void Main(string[] args)
{
//当s>50时,使用break语句终止循环;当s为偶数时,使用continue开始下一个循环
int s = 0;
int num = 100;
while (s < num)
{
s++;
if (s > 50)
{ break; }
if (s % 2 == 0)
{ continue; }
Console.Write(s+" ");
}
Console.WriteLine("\n 学敏,以上就是0到50的所有奇数的输出!请注意查收!");
Console.ReadLine();
}
}
}
运行结果:
4,for语句:声明一个int类型的数组,然后向数组中添加5个值,最后使用for循环语句遍历数组,并将数组中的值输出.
namespace for语句
{
class Program
{
static void Main(string[] args)
{
string[]xuemin;
xuemin=new string [5]; //声明一个具有5个元素的string型数组
xuemin[0] = "大家好!"; //向数组中添加元素
xuemin[1] = "我叫韩学敏";
xuemin[2] = "我喜欢学习计算机!";
xuemin[3] = "谢谢我的恩师—米新江!!!";
xuemin[4] = "我会好好努力的,投入百分之百的热情!";
for (int i = 0; i < xuemin.Length; i++) //利用for语句 输出数组中的每个元素
{
Console .WriteLine ("xuemin [{0}]的值为:{1}",i ,xuemin [i ]);
}
Console .ReadLine ();
}
}
}
5,foreach语句:遍历数组
namespace foreach语句
{
class Program
{
static void Main(string[] args)
{
int count;
Console.WriteLine("输入学敏的朋友个数");
count = int.Parse(Console.ReadLine());
string[] names = new string[count]; //声明数组names,数组的元素个数为输入的学敏的朋友个数count
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("请输入学敏的第{0}个朋友的姓名:", i + 1);
names[i] = Console.ReadLine();
}
Console.WriteLine("已经输出的学敏的朋友如下:");
foreach (string name in names) //foreach语句遍历数组
{
Console.WriteLine("{0}", name);
}
Console.ReadKey();
}
}
}
6,break语句:
在switch语句和while语句中的应用例子见上面1、2
在for语句中的应用如下:
namespace break语句
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console .Write ("\n 第{0}次循环:",i ); //输出提示第几次循环
for (int j=0;j <200;j ++)
{
if (j==12) //如果 j 的值等于12
break ; //终止循环
Console .Write (j+" "); //输出 j
}
}
Console .ReadLine ();
}
}
}
7,continue语句
在while语句中的使用见上例2.
在for语句中的使用大同小异.不再举例子了.
8,goto语句:通过goto语句实现程序跳转到指定语句。
namespace goto语句
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine ("请输入查询的文字:");
string inputstr=Console .ReadLine (); //将输入的文字赋值给inputstr变量
string[] Mystr=new string [5]; //声明一个string类型数组,数组名为Mystr
Mystr [0]="学敏今天很高兴!"; //为每个数组元素赋值
Mystr [1]="今天不高兴。";
Mystr [2]="后果很严重!";
Mystr [3]="学敏好饿啊!";
Mystr [4]="什么时候吃饭?";
for (int i=0;i <Mystr .Length ;i ++)
{
if (Mystr [i].Equals (inputstr )) //判断输入的文字是否在数组中存在,如果存在则跳转到Found语句
{ goto Found; }
}
Console .WriteLine ("您查找的“{0}”不存在",inputstr ); //如果不存在,则输出提示语句,并跳转到Finish语句
goto Finish;
Found:
Console .WriteLine ("您查找的“{0}”存在!",inputstr );
Finish:
Console .WriteLine ("\n 查找完毕" );
Console .ReadLine ();
}
}
}
9,return语句
namespace return语句
{
class Program
{
static string MyStr(string str)
{
string OutStr; //声明一个字符串变量OutStr
OutStr = "\n 韩总(韩学敏)您输入的数据是" + str; //为变量OutStr赋值
return OutStr; //使用return语句返回字符串变量OutStr
}
static void Main(string[] args)
{
Console.WriteLine("韩总,请您输入会议内容:"); //输出提示信息
string inputstr = Console.ReadLine(); //获取输入的数据,并赋给变量inputstr
Console.WriteLine(MyStr(inputstr)); //调用MyStr 方法,并将结果显示出来
Console.ReadLine();
}
}
}