C#整理2——C#的输入输出及基本类型

 

 //输出

 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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值