2017-2-17 c#基礎學習 (控制台程式的建立,輸出,輸入,定義變數,變數賦值,值覆蓋,值拼接,值列印)

來源:互聯網
上載者:User

標籤:ons   技術   string   height   parse   選擇   改名   main函數   分割線   

1 控制台程式的建立

> 建立項目  ,選擇 c#,  架構選擇4.0 , 選擇控制應用台程式, 選擇檔案儲存位置 修改名字。

2 c#輸出與輸入

>在main函數中編寫代碼

>在編寫時可以先插入Console.ReadLine();防止程式閃退

>

 Console.Write("執行個體語句");//不換行輸出            Console.WriteLine("樣本語句");//換行輸出            Console.ReadLine();//等待使用者輸入  防止閃退            

結果如下

3定義變數 賦值

  string a =" yaowei";//定義變數並賦值            Console.WriteLine(a);//輸出            Console.ReadLine();            string b = Console.ReadLine();//定義變數b等待使用者輸入資訊            Console.WriteLine(b);//輸出使用者輸入資訊            Console.ReadLine();

結果如下、

4值拼接  定義整形變數將字串變換成整型

string x = "yao";            string y = "wei";            string z = x + y;//值拼接            Console.WriteLine(z);//            int k = 1;            int l = 2;            int m = k + l;            Console.WriteLine(m);//輸出結果為3


 

>整型可以執行“+-*/”操作,結果是數學運算

 

 

練習題  “請輸入您的姓名:”同一行出現游標,等待使用者輸入
 “請輸入您的性別:”游標,等待使用者輸入
 “請輸入您的年齡:”同上
 “請輸入您的身高:”同上
 “請輸入您的體重:”同上
 “--------------------華麗的分割線-----------------------”
 xxx你好!您的性別是“男”,您的年齡是“18”,您的身高是“180”,您的體重是“180”。
 

  Console.Write("請輸入您的姓名:");//不換行輸出            string xingming = Console.ReadLine();//等待使用者輸入            Console.Write("請輸入您的性別:");            string xingbie =Console.ReadLine();            Console.Write("請輸入您的年齡:");            string  nianling=Console.ReadLine();            Console.Write("請輸入您的身高:");            string  shengao=Console.ReadLine();            Console.Write("請輸入您的體重:");            string tizhong =Console.ReadLine();            Console.WriteLine("----------------------------華麗的分割線------------------------------");            //定義變數  並賦值            string a = "您好!", b = "您的性別是“", c = "”,您的年齡是“", d = "”,您的身高是“", e = "”,您的體重是“", f = "”。";            string end = xingming + a + b + xingbie + c + nianling + d + shengao + e + shengao + f; //將所有語句與使用者輸入合并                Console.WriteLine(end);//列印最終結果                int x = int.Parse(shengao);                int y = int.Parse(tizhong);                int z = x + y;                string l = "您的身高和體重的和是“", k = "”。";                string he = l + z + k;                Console.WriteLine(he);            Console.ReadLine();

實際結果如下

個人理解  實際操作中不要盲目定義變數 先理清思路

注意實際運用中的標點符號

 

自己練習題落霞與孤鶩齊飛,   秋水共長天一色。(同行填空)
這首詩出自藤王閣序
恭喜你答對了!(第二行)
落霞與孤鶩齊飛,?秋水共長天一色。這句詩出自?藤王閣序。
-----------割------------
兩句詩多少個字?
詩名多少個字?
詩詞加詩名共18個字。

》》

代碼

  Console.Write("落霞與孤鶩齊飛,");//首行                      string shi = Console.ReadLine();//等待使用者輸入            Console.Write("這首詩出自");            string ming = Console.ReadLine();            Console.WriteLine("恭喜你答對了!");            string a = "落霞與孤鶩齊飛,", b = "。這首詩出自《", c = "》。";//定義變數            string end = a + shi + b + ming + c;//最終結果            Console.WriteLine(end);            Console.WriteLine("--------------割 --------------------");            Console.Write("兩句詩共多少個字?");            string x = Console.ReadLine();            Console.Write("詩名多少個字?");            string y = Console.ReadLine();            int k = int.Parse(x);//轉換整形變數            int l = int.Parse(y);            int z = k + l;            string u = "詩詞加詩名共", i = "個字。";            string p = u + z + i;            Console.WriteLine(p);            Console.ReadLine();


實際結果如下

 

2017-2-17 c#基礎學習 (控制台程式的建立,輸出,輸入,定義變數,變數賦值,值覆蓋,值拼接,值列印)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.