博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#整理2——C#的输入输出及基本类型
阅读量:4498 次
发布时间:2019-06-08

本文共 4548 字,大约阅读时间需要 15 分钟。

 

//输出 Console.WriteLine("摩西摩西"); Console.Write("hollo");不带回车的
View Code

 

注意:

 

1.注意大小写敏感。(快捷键操作)

 

2.括号,引号,分号都是英文状态下的符号。

 

3.结尾不要忘记写分号。

 

4.扳手后=。方块后()

 

string 字符串

 

//输入string n=Console.ReadLine();
View Code

 

 

 

如何拼接字符串?

 

Console.WriteLine("你的用户名是"+u+",密码是"+p+",请确认。");

 

 

//其他内容

Console.ForefroundColor = ConsoleColor.Red;  //设置文字颜色

Console.ForebankColor = ConsoleColor.Red;   //设置背景颜色

 

Console.Clear();   清屏

//显示

 

 

 

 

**********************************************************************

一、数据类型:

 

字符串(string)---放一串字符。需要用""引起来。

 

string a = "456"

string s = "789"

Console.Writeline(a+s);

 

整型(int4字节)---整数类型   不用引号     long(长整型8字节)    short(短整型2字节)    tiny(微整型1字节) 

1字节(B)=8位         1B=8b    1Byte = 8bit

1KB=1024B   1MB=1024KB  1GB=1024MB   1TB=1024GB

  

int a = 456;

int b = 789;

Console.Writeline(a+b);

 

小数型,浮点型(float,doubli)

float:单精度浮点型。                      4字节

double:双精度浮点型。(正常用)          8字节

 

double d = 3.14;

float c = 3.14f;

 

 

 

布尔型(bool):逻辑型,非此即彼。true,false;

 

bool b = true;

bool d = false;

 

 

字符型(char):单个字符    单引号

 

char c = '4';

 

 

二、变量(可改)---运行的过程中,值可以改变的量。

先定义,后使用。

定义变量名时,不能重名。

(一)定义:

 

1.什么叫做变量?

 

           我们把值可以改变的量叫做变量。

 

数据类型  变量名[=值];

int a;

int b = 29;

 

(二)赋值:

变量名 = 值;

使用赋值符号“=”给其赋上对应的值即可。 如: int number=100;

同时声明多个变量并在声明时赋值 如:int a=1,b=2,c=3;

 

(三)取值:

直接使用变量名,就可以把变量中存在的值给取出来。

 

(四)变量命名的一般规则

1.变量名一般由字母、数字、下划线组成。

2.变量名开头只能好似字母或下划线。

3.变量名不能与系统关键词重复。   关键词不能变蓝

(五)局部变量

 一定要:先声明,再复制,最后使用。

 

 

三、常量(不可改)---运行的过程中,值无法改变的量。

1.字面常量。

2.符号常量。   const int abc = 2;   定义:在变量定义的左边加上const关键字就可以了。

注意:符号常量在定义的时候必须赋值。

符号常量的应用:在一些反复使用的复杂数据,一般喜欢用常量来替代它,使用常量进行编程运算。

 

 

 

四、类型转换

计算机只能对同一类型数据进行运算,不同类型数据不能直接运算,如果是不同类型,则需要进行转换(自动,强制)。

 

int a =10;

double b = 3;

Console.WriteLine(a/b);  //先把a的值10变成小数类型10.0000,然后在做除法运算。

 

1.自动转换:由计算机根据运算数据自动进行类型转换。其原则是,只要从类型上来说,不会丢失数据,就转化。

 

tiny_>short_>int_>long_>double

 

2.强制转换:由程序员强制把某种类型,变成另一种类型。这种强制转换是在计算机不自动转化的时候实施,但有可能丢数据。

 

语法:

1.在被转换的数据左边加上小括号,小括号里面写上要转化的目标类型。

int a = (int)3.14

2.使用Convert.Toxxx()转化。

int a =10;

double b = 3.0;

int c = a/Convert.ToInt32(b);

 

string s = "7896";

int n = Convert.ToInt32(s);

 

例子1.:     

Console.Write("请输入你的年龄:");            string a = Console.ReadLine();            int s = Convert.ToInt32(a);            Console.WriteLine("你明年就"+(s+1)+"岁了");
View Code

 

 

例子2:1、 

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             int a = 10;              // C为整数13 14             int b = 3;15 16             int c = a / b;17 18             Console.WriteLine(c); 19         }20     }21 }
View Code

 

           2、 

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             int a = 10;              // C为小数13 14             double b = 3;15 16             double c = a / b;17 18             Console.WriteLine(c); 19         }20     }21 }
View Code

 

           3、

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12              int a = 10;            // C为整数13 14              double b = 3;15 16              double c = a / b;17 18              Console.WriteLine((int)c);19         }20     }21 }
View Code

 

  

           4、 

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             int a = 10;                 // C为整数13 14             double b = 3;15 16             int c = a / (int)b;17 18             Console.WriteLine(c);19         }20     }21 }
View Code

 

           5、

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             int a = 10;                     //C为整数13 14             double b = 3;15 16             int c = a /Convert.ToInt32(b);17 18             Console.WriteLine(c );19         }20     }21 }
View Code

 

例子3:把字符串变成整数

           

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace ConsoleApplication1 7 { 8     class Program 9     {10         static void Main(string[] args)11         {12             string a = "1233";13 14             int s = Convert.ToInt32(a);15 16             Console.WriteLine(s);17         }18     }19 }
View Code

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/viven/p/4311005.html

你可能感兴趣的文章
小div在大div中垂直居中,以及div在页面垂直居中
查看>>
有用的导航栏代码
查看>>
语法错误 : 缺少“;”(在“*”的前面) 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int...
查看>>
2015Web前端攻城之路
查看>>
推荐一个算法网站
查看>>
Python操作MySQL+Redis+MongoDB
查看>>
2017.6.30 Note replace innerHTML split() join()
查看>>
过滤关键词(中间有空格一样过滤)
查看>>
sql 当重复的数据有多条时,保留一条,删除其他重复
查看>>
RFC 4627 JSON
查看>>
Django入门与实践
查看>>
一些面试题(3)
查看>>
算法一枚
查看>>
Spin lock 与mutex 的区别--2011.01.06
查看>>
Java resources
查看>>
python--异常处理
查看>>
MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1
查看>>
数论只会GCD。。。
查看>>
UVA 12506 Shortest Names
查看>>
利用 jQuery 来验证密码两次输入是否相同
查看>>