1. 程式人生 > 實用技巧 >C#基礎總結(二)

C#基礎總結(二)

異常捕獲:

try

{

有可能出現錯誤的程式碼寫在這裡

}

catch

{

出錯後的處理

}

上面的程式如何執行:

如果try中的程式碼沒有出錯,則程式正常執行

try中的程式碼,不會執行catch中的程式碼;否則

直接跳到catch中執行程式碼!

210057609.png
210112214.png

210225167.png

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入你的姓名!");
                string name = Console.ReadLine();
                Console.WriteLine("請輸入你的語文成績:");
                int chinese = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("請輸入你的數學成績:");
                int maths = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("請輸入你的英語成績:");
                int english = Convert.ToInt32(Console.ReadLine());
                int sum = chinese + maths + english;
                double average =1.0* (chinese + maths + english) / 3;
                Console.WriteLine("{0}!你的總分數為{1},平均分為{2}", name, sum, average);
            }
            catch
            {
                Console.WriteLine("你輸入的資料有誤,請重新輸入:");
            }
            Console.ReadKey();
        }
    }



class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("請輸入你要計算的天數:");
           int number = Convert.ToInt32(Console.ReadLine());
           int week = number / 7;
           int day = number % 7;
           Console.WriteLine("{0}天是{1}周零{2}天",number,week,day);
       }
   }


class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("請輸入你要計算的時間(秒):");
           int second = Convert.ToInt32(Console.ReadLine());
           int day = second / (24 * 3600);
           int hour = second / 3600;
           int minute = second / 60;
           int second_1 = second % 60;
           Console.WriteLine("{0}秒是{1}天{2}小時{3}分{4}秒",second,day,hour,minute,second_1);
       }
   }


運算子

1.自加自減:

++ 自加一 有前加的後加 -- 自減一 有前減的後減 Int age = 18; int sum = age++ - 10; int sum = ++age - 10; int sum = age-- - 10; int sum = --age - 10;後加或後減原值計算,前加或前減新值計算。 然而age++ ++age --age age--都進行了自+1或是自-1。C#中,一般情況下,一元運算子的優先順序大於二元運算子。
int a = 1;
a++; // a = a + 1
int b = a;
a--; // a = a - 1
int c = a;
Console.WriteLine("a++ : {0}", b);
Console.WriteLine("a-- : {0}", c);

213602842.jpg

2.複合運算子:

+= 例如:age= age + 3;<==> age += 3; 在age上直接加3 -= 例如:age= age - 3; <==> age -= 3; 在age上直接減3 *= 例如:age= age * 3; <==> age *= 3; 在age上直接乘3 /= 例如:age= age / 3; <==> age /= 3; 在age上直接除3 %= 例如:age= age % 3; <==> age %= 3; 在age上直接與3取餘數

在C#中,能夠改變變數中值的有=/++/--

int a = 10;
a = a + 10; //將a 的值賦予 a(10) + 10
//運算完成後a = 20
Console.WriteLine(" a=a+10; a={0}",a);
a = 10; //在將a還原回10
a += 10; //等同於 a = a + 10; 將a 的值賦予 a(10) + 10
//運算完成後a還是等於 20
Console.WriteLine(" a+= 10; a={0}", a);

213739586.jpg


3.關係運算符(比較運算子)

> < //>表示大於號,<表示小於號。 == != //==兩個等號表示等於,一個等號表示賦值,!=表示不等於。 >= <= //>=表示大於等於,<=表示小於等於。 在C#中,有6個關係運算符,用於比較兩個事物之間的關係。 關係表示式:由關係運算符連線起來的式子就叫關係表示式。


4.布林(bool)型別:

bool值只有兩個 1.真:true 2.假:false 如果關係運算表示式成立,則這個表示式的值為true,否則為false.