VS输入输出基本操作以及数据类型和类型转换

 

(一)   C#项目的组成结构

 

  1. 项目结构

 

.config ---配置文件(存放配置参数文件)

 

.csproj ---项目文件(管理文件项)

 

.sln   ---解决方案文件(管理项目)

 

.cs   ---源文件(程序代码)

Namespace ConsoleApplication1    命名空间

Class Program                    类

static void Main(string[] arges)       方法

 

  1. 函数的四要素

 

名称,输入,输出,加工

 

输入,输出
注释:告诉电脑,不用当成代码去执行。
 // 注释到行尾,注释一行。
 /*sdfasdfasdf **/ 注释中间的内容,多行。

//输入
string s = Console.ReadLine();
//输出
Console.WriteLine("你刚才输入的是:"+s);


1.数字
Console.WriteLine(22); //22
Console.WriteLine(12+44); //56
2.文字
Console.WriteLine("abc");
Console.WriteLine("78"+"56");

3.整合输出
Console.WriteLine("78+56=" + (78 + 56));

Console.Write("78+56=");
Console.WriteLine(78 + 56);




做一个加法器:
显示:请输入一个数。
等待输入
显示:请输入另一个数
待待输出
显示最终的运算结果。

举例如下:

static void Main(string[] args)
{


//显示:请输入一个数。
Console.Write("请输入一个数:");


//等待输入
string a = Console.ReadLine();


//显示:请输入另一个数
Console.Write("请输入另一个数:");


//等待输出
string b = Console.ReadLine();


//显示最终的运算结果。
Console.Write(a+"+"+b+"="+(int.Parse(a)+int.Parse(b)));
}

项目一:做一个和电脑对话聊天的小程序:

1.static void Main(string[] args)
{
Console.WriteLine("小hi:您叫什么名字?");
Console.Write("我:");
string xm = Console.ReadLine();
Console.WriteLine("小hi:哦,原来你就是"+xm+"啊,久仰了!,你喜欢什么好吃的?");
Console.Write("我:");
string sw = Console.ReadLine();
Console.WriteLine("小hi:我也喜欢吃"+sw+",你能吃多少啊?");
Console.Write("我:");
string sl = Console.ReadLine();
Console.WriteLine("小hi:你居然吃"+sl+",比我吃得多多啦");

}

2.

static void Main(string[] args)
{
Console.WriteLine("小白:你好,你叫什么名字?");
Console.Write("我:");
string xm = Console.ReadLine();
Console.WriteLine("小白:原来你叫"+xm+"啊,好名字。你喜欢吃什么好吃的?");
Console.Write("我:");
string sw = Console.ReadLine();
Console.WriteLine("小白:原来你也爱吃"+sw+"啊,我也挺喜欢吃的,你一次吃多少啊?");
Console.Write("我:");
string sl = Console.ReadLine();
Console.WriteLine("小白:原来你能吃"+sl+"啊,比我吃的多,改天我们一起去吃吧。你平时喜欢做什么运动?");
Console.Write("我:");
string yd = Console.ReadLine();
Console.WriteLine("小白:哦,你喜欢"+yd+"啊,我也喜欢这个运动,有时间可以一起去玩吧。");

Console.WriteLine("小白:很高兴认识你,和你聊天很开心,期待下次和你见面。");

}

项目二:做一个信息调查问卷

static void Main(string[] args)
{
Console.WriteLine("☞……………信息调查表………………☜");//显示标题
Console.Write("姓名:");
string xm = Console.ReadLine();
Console.Write("性别:");
string xb = Console.ReadLine();
Console.Write("毕业学校:");
string byxx = Console.ReadLine();
Console.Write("所学专业:");
string sxzy = Console.ReadLine();
//显示
Console.WriteLine("**********您刚才输入的内容如下**************");
Console.WriteLine("姓名:"+xm+" 性别"+xb);
Console.WriteLine("毕业学校:" + byxx );
Console.WriteLine("所学专业:" + sxzy);
Console.ReadLine();

}

2016.4.11下午主要讲了数据类型和基本的类型转换

2016.4.11  下午

一.数据类型

   1.基本数据类型

                        注:字节:例{10221021  8位数为一个字节    8b=1B}

                       1).整型(整数)

                                 ① short(比Int短   Int16){2个字符数/2字节} 

                                 ② Int(重点  Int32){4个字符数/4字节}   最多可显示比电话号码少一位的字符数  最常用的

                                 ③ long(比Int长  Int64){8个字符数/8字节} 

                       2).浮点型(非整数、小数)

                                 ① float 如果小数要作为float看待,需要在后面加上f,F的后缀'(比double的小数点后位数要少  如:3.5  5.6){4个字节}                                    

                                举例:float  a = 3.14f;      float a = 3.14;//错

                                 ② double(重点)   默认小数都是double  (小数点后面的位数如:3.555   5.2222222){8个字节}  最常用的   

                                举例:double c = 3.14;

                                 ③ decimal(比double的小数点后位数要多 如:3.555555555555555省略){16个字节}

                       3).字符型(单字符型  只能输入单个字符  如:A 、 B、  a、  b )

                                 ① char   用单引号,只能放一个字符。转义字符除外  如:Char a = 'A';{但括号内只能是单个字符}

                             代码举例:

                               char c = 'A';      char c = "A";//错 char c='ABC';//错             

                       4).布尔型(开关型、对错型、只能二选一的)

                                 ① bool  只能放true,false。代码中只能是小写,而且不要加引号。  

                         代码举例:  

                           bool g = true;   bool g = True;//错      bool g = "true";//错

   2.引用类

                       1).字符串

                                   string  起串联作用  就像串糖葫芦的竹签  把单个的字节给串联起来形成的就叫字符串 

                                             用双引号包括   一个或多个字符组成

                             举例:string s = "abcdef";   string s = "a";    string s = 'asdfasdf';//错
                       

二.字符转换

                       1).C#转义字符  

                                    \" 双引号       \\ 反斜杠      \n 换行      \r回车(注:此处回车是回到本行的首端,并不换行,如果前段有字符,后面的字符会遮盖住前段的字符)

                                    \t 水平制表符(Tab键)

                                            在编码时  碰到需要输出的是特殊组合时 用反斜杠分开   如:

                                           1. Console.WriteLine("C:\ners\rvcy\aaa");此时C#运行时会按照上面的转义字符进行转义  并不能正确显示我们想要的内容 正确的是

                                            Console.WriteLine("C:\\ners\\rvcy\\aaa");  此时运行C#就会正确显示Console.WriteLine("C:\ners\rvcy\aaa");并不执行转义字符的转义

                                              2.  string s = "注意:\"小狗\"是用来表示亲昵的称乎";
                                                 Console.WriteLine(s);

                       2).基本类型转换

                                  ① 自动转换      C#在进行转换时 同一数据间 不可能存在数据丢失的情况下 C#自动进行转换(隐式转换:C#后台自动转换

                                  ② 强制转换      

                                对于数字:在被转换的值的左边加上小括号,在小括号中写上被转成哪种类型。
                                   float a = (float)3.14;
                                对于字符串:字符串转成相对应的基本类型
                               法一:int a = int.Parse("字符串"); float b = float.Parse("字符串"); double c = double.Parse("字符串");bool d = bool.Parse("字符串")
                               法二:使用Convert
                              double d = Convert.ToDouble(s);

                               float f = Convert.ToSingle(s);
                               int a = Convert.ToInt32(s);

 

                             举例:

                                1.static void Main(string[] args)
                           {
                             Console.Write("您今年几岁了?");
                             string s = Console.ReadLine();
                             double age1 = Convert.ToDouble(s);
                             int age2 = (int)age1;
                             Console.WriteLine("您今{1}岁,明年就{0}岁", age2 + 1,age2);
                            }

 

                          2.static void Main(string[] args)
                       {

                          string s = "true";
                           bool b = Convert.ToBoolean(s);

                           b = !b;

                          Console.WriteLine(b);

                    }                                 

 

快捷键用法:

shift +tab 左移

tab 右移

ctrl+F5 执行不调试

ctrl+E,C 注释选中行

ctrl+E,U 取消对选中行的注释

alt+右箭头 自定定位

转载于:https://www.cnblogs.com/kellybutterfly/p/5380511.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值