2017-2-17 c#基础学习 (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

时间:2023-07-13 15:48:44

2017-2-17  c#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

1 控制台程序的创建

> 新建项目  ,选择 c#,  框架选择4.0 , 选择控制应用台程序, 选择文件保存位置 修改名字。

2 c#输出与输入

>在main函数中编写代码

>在编写时可以先插入Console.ReadLine();防止程序闪退

>

 Console.Write("实例语句");//不换行输出
Console.WriteLine("示例语句");//换行输出
Console.ReadLine();//等待用户输入 防止闪退

结果如下2017-2-17  c#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

3定义变量 赋值

  string a =" yaowei";//定义变量并赋值
Console.WriteLine(a);//输出
Console.ReadLine();
string b = Console.ReadLine();//定义变量b等待用户输入信息
Console.WriteLine(b);//输出用户输入信息
Console.ReadLine();

结果如下2017-2-17  c#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

4值拼接  定义整形变量将字符串变换成整型

string x = "yao";
string y = "wei";
string z = x + y;//值拼接
Console.WriteLine(z);//
int k = ;
int l = ;
int m = k + l;
Console.WriteLine(m);//输出结果为3

2017-2-17  c#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

>整型可以执行“+-*/”操作,结果是数学运算

练习题  “请输入您的姓名:”同一行出现光标,等待用户输入
 “请输入您的性别:”光标,等待用户输入
 “请输入您的年龄:”同上
 “请输入您的身高:”同上
 “请输入您的体重:”同上
 “--------------------华丽的分割线-----------------------”
 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();

实际结果如下

2017-2-17  c#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)

个人理解  实际操作中不要盲目定义变量 先理清思路

注意实际运用中的标点符号

自己练习题落霞与孤鹜齐飞,   秋水共长天一色。(同行填空)
这首诗出自藤王阁序
恭喜你答对了!(第二行)
落霞与孤鹜齐飞,?秋水共长天一色。这句诗出自?藤王阁序。
-----------割------------
两句诗多少个字?
诗名多少个字?
诗词加诗名共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#基础学习   (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)