三、流程控制

一、流程控制

  • 分支語句
    • if語句 (需要注意的是,與C/C++不同,C#中if語句的條件表達(dá)式必須等于布爾值。)

    • switch語句

    • 三元運(yùn)算符

  • 循環(huán)語句
    • do while語句
    • while語句
    • for語句
    • foreach語句
  • 跳轉(zhuǎn)語句
    • break語句
    • continue語句
    • goto語句
    • (return語句、throw語句拋出異常)

二、練習(xí)題

  • 1、編寫一個(gè)程序,對(duì)輸入的4個(gè)整數(shù),求出其中的最大值和最小值,并顯示出來。
             int I,T;
             Console.WriteLine("請(qǐng)輸入四個(gè)數(shù)");
             int a=Convert.ToInt32(Console.ReadLine());
             int b = Convert.ToInt32(Console.ReadLine());
             int c = Convert.ToInt32(Console.ReadLine());
             int d = Convert.ToInt32(Console.ReadLine());
             I = a >= b ? a : b;
             I = I >= c ? I : c;
             I = I >= d ? I : d;
             Console.WriteLine(I);
             T = a <= b ? a : b;
             T = T <= c ? T : c;
             T = T <= d ? T : d;
             Console.WriteLine(T);
  • 2、讓用戶輸入兩個(gè)整數(shù),然后再輸入0-3之間的一個(gè)數(shù),0代表加法,1代表減法,2代表乘法,3代表除法,計(jì)算這兩個(gè)數(shù)字的結(jié)果。
            int a;
            float num;
            Console.WriteLine("請(qǐng)輸入兩個(gè)整數(shù)");
            int I1 = Convert.ToInt32(Console.ReadLine());
            int I2= Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請(qǐng)輸入0到3之間的任意一個(gè)數(shù)");
            int I=Convert.ToInt32(Console.ReadLine());
            a = I - 0;
            if(a==0)
            {
                num = I1 + I2;
            }
            else if(a==1)
            {
                num = I1 >= I1 ? I1 - I2 : I2 - I1;
            }
            else if(a==2)
            {
                num = I1 * I2;
            }
            else
            {
                num = I1 >= I1 ? I1 / I2 : I2 /I1;
            }
            Console.WriteLine(num);
  • 3、求出1~1000之間的所有能被7整除的數(shù),并計(jì)算和輸出每5個(gè)的和。
            int I = 1;
            int num= 0;
            int A = 0;
            while(I<=1000)
            {
                if (I % 7 == 0)
                {
                    Console.WriteLine(I);
                    num += I;
                    A++;
                    if (A % 5 == 0)
                    {
                        Console.WriteLine("每五個(gè)和為"+num);
                        A = 0;
                        num = 0;
                    }
                }
                I++;
            }
  • 4、編寫一個(gè)控制臺(tái)程序,分別輸出1~100之間的平方、平方根。
            int I=0;
            double number1;
            double number2;
             for (I = 0; I <= 100; I++)
             {
                 number1 = Math.Pow(I,2);
                 number2 = Math.Sqrt(I);
                 Console.WriteLine(number1);
                 Console.WriteLine(number2);
             }
  • 5、編程輸出1~100中能被3整除但不能被5整除的數(shù),并統(tǒng)計(jì)有多少個(gè)這樣的數(shù)。
            int I;
            int number = 0;
            for (I = 1; I <= 100; I++) 
                {
                    if (I % 3 == 0 && I % 5 != 0)
                    {
                        Console.WriteLine(I);
                        number++; 
                    }
                }
                Console.WriteLine(number);
  • 6、編程輸出九九乘法表
            int a;
            int b;
            for(a=1;a<10;a++)
            {
                for(b=1;b<10;b++)
                {
                    if (b <= a)
                    {
                        Console.Write("{0}*{1}={2}\t", b, a, b* a);
                    }
                }
                Console.WriteLine();
            }
  • 7、輸出1~5的平方值,要求:用for語句實(shí)現(xiàn)。用while語句實(shí)現(xiàn)。用do-while語句實(shí)現(xiàn)。
            //for語句
            double b = 0;
            for (int a = 1; a < 6; a++)
            {
                b = Math.Pow(a, 2);
                Console.WriteLine(b);
            }
            //while語句
            int a = 1;
            double b = 0;
            while(a<6)
            {
                b = Math.Pow(a, 2);
                a++;
                Console.WriteLine(b);
            }
            //do-while語句
            int a = 1;
            Double b = 0;
            do
            {
                b = Math.Pow(a, 2);
                Console.WriteLine(b);
                a++;
            } while (a < 6);
  • 8、要求用戶輸入5個(gè)大寫字母,如果用戶輸入的信息不滿足要求,提示幫助信息并要求重新輸入。
           while (true)
            {
                Console.WriteLine("請(qǐng)輸入5個(gè)大寫字母");
                string str = Console.ReadLine();
                if (str.Length == 5)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (str[i] >= 'A' && str[i] <= 'Z')
                        {
                            if(i==4)
                            {
                                Console.WriteLine("您輸入的五個(gè)字母均為大寫");
                                break;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            Console.WriteLine("您輸入的字母中有非大寫請(qǐng)從新輸入");
                            break;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("您輸入的字母個(gè)數(shù)不對(duì)請(qǐng)重新輸入");
                }
            }
  • 9、一個(gè)控制臺(tái)應(yīng)用程序,要求完成寫列功能。
    1)接收一個(gè)整數(shù)n。
    2)如果接收的值n為正數(shù),輸出1~n間的全部整數(shù)。
    3)如果接收的值n為負(fù)值,用break或者return退出程序。
    4)如何n為0的話 轉(zhuǎn)到1繼續(xù)接收下一個(gè)整數(shù)。*/
            int num ;
            a1:
            Console.WriteLine("請(qǐng)輸入一個(gè)整數(shù)");
            num  =Convert.ToInt32(Console.ReadLine());
            if(num>0)
            {
                for(int n=1;n<=num;n++)
                {
                    Console.WriteLine(n);
                }
            }else if(num<0)
            {
                return;
            }else
            {
                goto a1;
            }
  • 10、給定一個(gè)分?jǐn)?shù),使用if語句,根據(jù)如下規(guī)則對(duì)分?jǐn)?shù)評(píng)級(jí),并輸出級(jí)別:1、0-29分為差;2、30-59分為不及格;3、60-69分為及格;4、70-89分為良好;5、90-100分為優(yōu)秀。
 int I = Convert.ToInt32(Console.ReadLine());
            if (I >= 90)
            {
                Console.WriteLine("優(yōu)秀");
            }
            else if (I >= 70 && I < 90)
            {
                Console.WriteLine("良好");
            }
            else if (I < 70 && I >= 60)
            {
                Console.WriteLine("及格");
            }
            else if (I < 60 && I >= 30)
            {
                Console.WriteLine("不及格");
            }
            else
            {
                Console.WriteLine("差");
            }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容