1. 程式人生 > >c#筆記2018-12-27

c#筆記2018-12-27

using System;
/*2018-12-27 c#學習筆記
 * 1.c#判斷if /else if /switch
 * 2.迴圈while/for/do-while
 * 3.迴圈例項: for迴圈99乘法表 /while 迴圈99乘法表/do while 迴圈99乘法表
 */
namespace Csharp_study
{
    class section2
    {
        static void Main(string[] args) {
            //1.c#判斷if /else if /switch
            double score = 67;
            double score2= 91;
            if (score > 60)
            {
                Console.WriteLine("及格");
            }
            else {
                Console.WriteLine("不及格");
            }
            //else if
            if (score2<60) {
                Console.WriteLine("不及格");
            }
            else if (score2>90)
            {
                Console.WriteLine("優秀");
            }
            else {
                Console.WriteLine("一般");
            }
            //switch 
            string  score3 = "A";
            switch (score3){
                case "A":
                    Console.WriteLine("優秀!");
                    break;
                case "B":
                    Console.WriteLine("一般");
                    break;
                case "D":
                    Console.WriteLine("不合格");
                    break;
                default:
                    Console.WriteLine("合格");
                    break;
            }
            //三目運算子?: 等同於if else
            string jieguo = (score < 60) ? "及格" : "不及格";
            Console.WriteLine(jieguo);


            //2.迴圈
            //while
            int i = 10;
            while (i > 0) {
                Console.WriteLine(i);
                i--;
            }

            //do while
            int j = 0;
            do {
                j++;
                Console.WriteLine(j);
            }while (j<10);

            //for迴圈
            Console.WriteLine("20以內3的倍數:");
            for (int z = 1; z < 20; z++) {
                if (z % 3 == 0) {
                    Console.WriteLine(z);
                }
            }
            //3.迴圈例項: for迴圈99乘法表 /while 迴圈99乘法表/do while 迴圈99乘法表

            //for迴圈
            for (int x = 1; x <=9;x++ )
            {
                for (int z = 1; z<=x; z++) {
                    Console.Write("{0}*{1}={2}\t",z,x,x*z);
                }
                Console.Write("\n");
            }

            //while 迴圈
            Console.Write("-----------------------------------------------------------------\n");
            int x2 = 1;
            while (x2<=9) {//外層迴圈
                int y2=1;
                while (y2 <= x2) {//內層迴圈
                    Console.Write("{0}*{1}={2}\t", y2, x2, x2 * y2);
                    y2++;
                }
                Console.Write("\n");    
                x2++;
            }

            //do while 迴圈
            Console.Write("-----------------------------------------------------------------\n");
            int aa = 1;
            do{//外層迴圈
                int bb=1;
                do{//內層迴圈
                    Console.Write("{0}*{1}={2}\t", aa, bb, aa * bb);
                    bb++;
                }while(bb<=aa);
                aa++;
                Console.Write("\n");
            } while (aa <=9);


             Console.ReadKey();
        }
    }
}