C#編程(1_初識運算式和變數)

來源:互聯網
上載者:User

標籤:winform   style   blog   color   io   os   ar   使用   for   

  • 在控制台應用程式中輸出“The frist app in Beginning C# programming!”

 

 1 namespace ConsoleApplication1 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             Console.WriteLine("The frist app in Beginning C# programming!"); 8             Console.ReadKey(); 9         }10     }11 }

 

  • 建立一個WinForm應用程式,設定一個Button,實現點擊這個Button,開啟一個訊息對話方塊的功能

 

 1 namespace WindowsFormsApplication1 2 { 3     public partial class Form1 : Form 4     { 5         public Form1() 6         { 7             InitializeComponent(); 8         } 9 10         private void button1_Click(object sender, EventArgs e)11         {12             MessageBox.Show("這是一個WinForm應用程式!");13         }14     }15 }
  • 聲明兩個變數;給這兩個變數賦初值,並將兩個變數的值輸出到控制台上

 

 1 namespace ConsoleApplication1 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             //聲明兩個變數,一個int類型變數,一個string類型變數 8             int myInteger; 9             string myString;10 11             //分別為已聲明的變數賦初值12             myInteger = 1989;13             myString="myInteger is ";//如需換行,使用分行符號“/n”14 15             Console.WriteLine("{0}{1}",myString,myInteger);//字串實際上是插入變數內容的一個模板,字串中的每對花括弧都是一個預留位置,包含列表中每個變數的內容16             Console.ReadKey();17         }18     }19 }

變數的命名:

  1. 基本規則:(1)變數名的第一個字元必須是字母、底線"_"或@;(2)其後的字串可以是字母、底線或數字;(3)不與系統關鍵字重名;(4)C#區分大小寫.
  2. 通常,我們根據變數的作用來命名它們.
  3. 目前.NET命名空間中有兩種命名規範PascalCase與camelCase:(1)PascalCase規則:變數名稱中的每個單詞除了第一個字母大寫,其餘全小寫;(2)camelCase規則:第一個單詞以小寫字母開頭.

      PascalCase變數名(舉例):Age;LastName;TimeOfDeath;

      camelCase變數名(舉例): age;firstName;timeOfDeath;

      Microsoft建議:簡單的變數使用camelCase規則,對於比較進階的命名規則則使用PascalCase規則.

變數使用前必須初始化,上面的指派陳述式可以用作初始化語句。

變數的類型(上面提到了int類型和string類型,還有其他許多類型,後面再學習

字串是參考型別(關於實值型別和參考型別,後面再學習),字串也可以被賦null值,即字串變數不引用字串。

  •  編寫程式,任意輸入兩個數,請輸入其加減乘除運算運算式及預算結果
 1 namespace ConsoleApplication1 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             //定義變數 8             double firstNumber, secondNumber; 9             string userName;10 11             //變數賦初值,分別讀取使用者輸入的內容並儲存到相應的變數中12             Console.WriteLine("Enter you name:");13             userName=Console.ReadLine();14             Console.WriteLine("Welcome {0}!",userName);15             Console.WriteLine("Give me a number:");16             firstNumber=Convert.ToDouble(Console.ReadLine());17             Console.WriteLine("Now Give me anther number:");18             secondNumber=Convert.ToDouble(Console.ReadLine());19             //輸出(變數運算)20             Console.WriteLine("{0}+{1}={2}", firstNumber, secondNumber, firstNumber + secondNumber);21             Console.WriteLine("{0}-{1}={2}", firstNumber, secondNumber, firstNumber - secondNumber);22             Console.WriteLine("{0}*{1}={2}", firstNumber, secondNumber, firstNumber * secondNumber);23             Console.WriteLine("{0}/{1}={2}", firstNumber, secondNumber, firstNumber / secondNumber);24             Console.ReadKey();25         }26     }27 }

 類型轉換(上面的例子用到了Convert,它的詳細的用例,後面再學習

 namespace名稱空間(是為了唯一地標識代碼及其內容而提供應用程式代碼容器的一種方式,名稱空間通常採用PascalCase規則命名,關於名稱空間的嵌套、Using的使用等等詳細的內容,後面再學習

C#編程(1_初識運算式和變數)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.