C#基礎入門第四天(循環判斷)

開篇前言:因爲年末的關係,工做上事太多,週末又被公司拉着出去應酬,這也許是我這個年齡段要自學一個東西作大的障礙,今天起,加快學習進度。ide

一:異常捕獲
異常:語法上沒有錯誤,在程序運行時,由於某些緣由出現錯誤,致使程序不能正常運行。
爲使程序更加健康好用,咱們應該多使用try-catch
語法
try
{
可能出現異常的代碼;
}
catch
{
出現異常後執行的代碼;
}學習

執行過程:try中的代碼沒有出現異常,則catch裏面不會自行,若是try中代碼出現異常,則後面的代碼都不執行,直接跳到catch中的代碼執行。
Console.WriteLine("輸入一串數字");
try
{
int number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("輸入的內容,不能被轉換爲數字!");
}
Console.WriteLine(number 2);
Console.ReadKey();
上面的代碼,Console.WriteLine(number
2);裏面的Number會報錯:變量未賦值,這裏就涉及到變量的做用域問題。
變量做用域:
變量的做用域就是你可以使用到這個變量的範圍。
變量的做用域通常從聲明它的那個括號開始到那個括號所對應的結束的括號結束。
在這個範圍內,咱們能夠訪問並使用變量。超出這個範圍就訪問不到了code

//try做用域外聲明變量,這樣就能被訪問到,這裏還有一個問題,即輸出不能被轉換的代碼後,依然會在輸出一個0的結果,由於下面那句是在外面的
int number = 0;//在外面定義變量
Console.WriteLine("輸入一串數字");
try
{
number = Convert.ToInt32(Console.ReadLine());//賦值
}
catch
{
Console.WriteLine("輸入的內容,不能被轉換爲數字!");
}
Console.WriteLine(number * 2);//使用
Console.ReadKey();視頻

上面的代碼因爲錯誤也會多輸出一行0,因此須要作以下更改,使輸入須要知足必定條件才能。
//聲明一個bool變量
bool b = true;
//try做用域外聲明變量,這樣就能被訪問到,這裏還有一個問題,即輸出不能被轉換的代碼後,依然會在輸出一個0的結果
int number = 0;//聲明一個變量
Console.WriteLine("輸入一串數字");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("輸入的內容,不能被轉換爲數字!");
b = false;//不知足條件把bool設置爲假
}
//要執行這段代碼,須要知足必定的條件
//讓代碼知足某些條件才執行,使用bool變量
if (b)//知足bool值爲真才執行
{
Console.WriteLine(number * 2);
}
Console.ReadKey();ci

2、swicth - case
用來處理多條件定值判斷
語法
switch(變量或者表達式的值)
{
case 值1:要執行的代碼;
break;
case 值2:要執行的代碼;
break;
case 值3:要執行的代碼;
break;
..........
default:要執行的代碼;
break;
}
執行過程:程序執行到switch處,首先將括號中變量或者表達式的值計算出來,
而後拿着這個值依次跟每一個case後面所帶的值進行匹配,一旦匹配成功,則執行
該case所帶的代碼,執行完成後,遇到break。跳出switch-case結構。
若是,跟每一個case所帶的值都不匹配。就看當前這個switch-case結構中是否存在
default,若是有default,則執行default中的語句,若是沒有default,則該switch-case結構
什麼都不作。作用域

if-else-if方法,這個推薦:多條件區間判斷使用
bool b = true;
decimal wages = 5000;
Console.WriteLine("輸入評級A-E");//A B C D E 亂七八糟
string level = Console.ReadLine();
if (level == "A")
{
wages += 500;
}
else if (level == "B")
{
wages += 200;
}
else if (level == "C")
{
}
else if (level == "D")
{
wages -= 200;
}
else if (level == "E")
{
wages -= 500;
}
else
{
Console.WriteLine("輸入有誤,退出程序");
b = false;
}
if (b)
{
Console.WriteLine("李四來年工資爲{0}", wages);
}
Console.ReadKey();string

switch-case方法,推薦多條件定值判斷
bool b = true;
decimal wages = 5000;
Console.WriteLine("輸入評級");
string level = Console.ReadLine();
switch (level)
{
case "A":wages += 500;
break;
case "B":wages += 200;
break;
case "C":break;
case "D":wages -= 200;
break;
case "E":wages -= 500;
break;
default:Console.WriteLine("輸入有誤,退出程序!");
b = false;
break;
}
if (b)
{
Console.WriteLine("李四來年工資爲:{0}", wages);
}
Console.ReadKey();it

綜合練習:題目:按照用戶輸入的分數,給出評級,>=90給A,80B,70C,60D,60如下E
int score = 0;
Console.WriteLine("輸入一個考試成績");
try
{
score = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("輸入有誤,退出程序");
}
switch (score/10)
{
case 10:
case 9:Console.WriteLine("A級");
break;
case 8:Console.WriteLine("B級");
break;
case 7:Console.WriteLine("C級");
break;
case 6:Console.WriteLine("D級");
break;
default:Console.WriteLine("E級");
break;
}
Console.ReadKey();console

綜合練習2:要求輸入年份,在輸入月份,根據輸入給出當月天數
Console.WriteLine("請輸入年份");
try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入月份");
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 29;
}
break;
default:
day = 30;
break;
}
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}//判斷月份數字是否正常的if結尾
else
{
Console.WriteLine("輸入的月份有誤,退出程序");
}
}//月份try的結尾
catch
{
Console.WriteLine("輸入的月份有誤,退出程序");
}
}//年份try的結尾
catch
{
Console.WriteLine("輸入年份有誤,退出程序!");
}
Console.ReadKey();class

3、while循壞結構
語法
while(循環條件)
{
循環體;
}
執行過程:程序運行到while處,首先判斷while所帶的小括號內的循環條件是否成立,
若是成立的話,也就是返回一個true,則執行循環體,執行完一遍循環體後,再次回到
循環條件進行判斷,若是依然成立,則繼續執行循環體,若是不成立,則跳出while循環。
在while循環當中,通常總會有那麼一行代碼,可以改變循環條件,使之終有一天再也不成立,
若是沒有那麼一行代碼可以改變循環條件,也就是循環條件永遠都成立,咱們稱之這種循環
叫作死循環。
最簡單的最經常使用的死循環:
while(true)
{

}

特色:先判斷,再執行,有可能一遍循環都不執行。

//向控制檯打印10遍
//循環體:console.writeLine("一遍又一遍")
//循環條件:打印10遍

//定義一個循環變量來記錄循環的次數,每循環一次,變量自增1
        int number = 0;
        while (number < 10)
        {
            Console.WriteLine("一遍又一遍{0}",number);
            number++;//每循環一遍,變量自增1
        }
        Console.ReadKey();

while循環小練習
/
求1-100每一個數加起來的和
循環體 1+2+.....
循環條件:number <=100
/
int i = 1;
int sum = 0;
while (i <= 100)
{
sum += i;
i++;
}
Console.WriteLine(sum);
Console.ReadKey();

/*
         求1-100每一個偶數加起來的和
         循環體 累加的過程
         循環條件:number <=100
         */
        int i = 1;
        int sum = 0;
        while (i <= 100)
        {
            if (i % 2 == 0)
            {
                sum += i;
            }
                i++;
        }
        Console.WriteLine(sum);
        Console.ReadKey();

        /*
         求1-100每一個奇數加起來的和
         循環體 累加的過程
         循環條件:number <=100
         */
        int i = 1;
        int sum = 0;
        while (i <= 100)
        {
            if (i % 2 != 0)
            {
                sum += i;
            }
            i++;
        }
        Console.WriteLine(sum);
        Console.ReadKey();

4、break關鍵字的做用
1)、能夠跳出switch-case結構。
2)、能夠跳出當前循環。
break通常不單獨的使用,而是跟着if判斷一塊兒使用,表示,當知足某些條件的時候,就再也不循環了。

5、while循環的綜合練習
/
輸入班級人數,而後一次輸入學員成績,計算班級學員的平均成績和總成績
/
//循環體:提示輸入學員成績,轉成整數型,累加到總成績中
//循環條件:輸入次數小於等於班級人數
Console.WriteLine("請輸入班級人數!");
int sum = 0;
int i = 1;//聲明一個變量用來存儲循環的次數
int NumberOfPeople = Convert.ToInt32(Console.ReadLine());
while (i<=NumberOfPeople)
{
Console.WriteLine("請輸入第{0}成績!",i);
int score = Convert.ToInt32(Console.ReadLine());
sum += score;//把每一個學員成績累加到總成績
i++;
}
Console.WriteLine("平均成績是{0},總成績是{1}", sum / NumberOfPeople, sum);
Console.ReadKey();

/
老師問學生,這道題你會作嗎?若是學生回答「會了(y)」則能夠放學,若是學生回答不會(n),
則老師在講一遍,在問學生會不會
一、直到學會爲止,才能夠放學
二、直到學生會或者老師講10遍還不會,放學
這題第一種是本身想的,反向是根據視頻老師的作法
/
string student = "";
while (student != "y")
{
Console.WriteLine("老師講一遍並問道:你會了嗎?");
student = Console.ReadLine();
}
Console.WriteLine("好的,你會了,放學!");
Console.ReadKey();

bool b = true;
        int second = 0;
        string student = "";
        while (student != "y")
        {
            if (second != 10)
            {
                Console.WriteLine("老師講一遍並問道:你會了嗎?");
                student = Console.ReadLine();
                second++;
            }
            else
            {
                Console.WriteLine("十遍,老師被氣死了!");
                b = false;
                break;
            }
        }
        if (b)
        {
            Console.WriteLine("好的,會了就放學!");
        }
        Console.ReadKey();
                    //反向作法
        string student = "";
        int i = 1;
        while (student != "y" && i <= 10)
        {
            Console.WriteLine("這是老師第{0}幾遍講,你會了嗎?", i);
            student = Console.ReadLine();
            if (student == "y")
            {
                Console.WriteLine("會了就放學!");
                break;
            }
            i++;
        }
        Console.ReadKey();

                     /*
         2006年培養學員80000人,每一年增加25%,請按照此增加速度,到哪一年培訓學員人數達到20萬人?PS:這題開始我把判斷條件寫反了,看了十多分鐘才發現問題
         */
        //循壞體:在原基礎上,學員每一年增加25%累加
        //循環條件:達到20W
        double student = 80000;
        int year = 2006;
        while (student <= 200000)
        {
            Console.WriteLine("如今有{0}人", student);
            student += Convert.ToInt32(student * 0.25);
            Console.WriteLine("通過一年有{0}人", student);
            year++;
        }
        Console.WriteLine("{0}年,人數達到20W", year);
        Console.ReadKey();

                    /*
         提示輸入用戶名和密碼 用戶名爲admin密碼888888
         只要用戶名或者密碼錯誤就從新輸入
         最多能輸入10此
         */

        //循環體,讓用戶輸入用戶名和密碼
        //10次,或者輸對
        int i = 1;
        string useName = "";
        string usePws = "";
        while ((useName != "admin" || usePws != "888888")&&i<=3)
        {
            Console.WriteLine("輸入用戶名");
            useName = Console.ReadLine();
            Console.WriteLine("輸入密碼");
            usePws = Console.ReadLine();
            i++;
        }
        Console.ReadKey();

                    二、第二個循環用戶B的用戶名不能跟A同樣,或者爲空,
         //循環體:提示用戶B輸入用戶名  接收  判斷
         //用戶名爲空 或者跟A相同
         */
        Console.WriteLine("請輸入用戶名,不能爲空");
        string userNameA = Console.ReadLine();
        while (userNameA == "")
        {
            Console.WriteLine("用戶名爲空,從新輸");
            userNameA = Console.ReadLine();
        }
        Console.WriteLine("請輸入用戶名,不能爲空");
        string userNameB = Console.ReadLine();
        while (userNameB == "" || userNameB == userNameA)
        {
            if (userNameB == "")
            {
                Console.WriteLine("用戶名不能爲空,從新輸入!");
                userNameB = Console.ReadLine();
            }
            else
            {
                Console.WriteLine("用戶名重複,從新輸入!");
                userNameB = Console.ReadLine();
            }
        }

6、do-while循環結構語法:do{循環體;}while(循環條件);執行過程:程序首先會執行do中的循環體,執行完成後,去判斷do-while循環的循環條件,若是成立,則繼續執行do中的循環體,若是不成立,則跳出do-while循環。特色:先循環,再判斷,最少執行一遍循環體。

相關文章
相關標籤/搜索