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();
}
}
}