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();
實際結果如下