0
点赞
收藏
分享

微信扫一扫

C#自学Day 02

Sophia的玲珑阁 2022-03-26 阅读 32
unityc#

前言

今天又接到几家公司的笔试邀请,本来的话我是打算不接受邀请了,这一段时间就去学习C#,但是我想着接受的话也可以检验一下学习成果,所以就边学边做吧!虽然今天依旧是小白,但是今天和昨天的自己不一样啦!加油,爱学习的时小糖!

C#自学Day 02

1.注释符

2.常用快捷键

3.变量

3.1.变量的引入

3.2变量的类型

3.3变量的写法

//官方解释:声明一个int类型的变量
int number; //变量类型;变量名;简单理解就是开一个房号是number的int类型房间
//官方解释:将100的值赋给变量number
number =100;//变量名=值;简单理解就是让100住进去number房间中
//或者简写
int n =100;

3.4变量的类型

4.变量的使用规范

int number ;
number = 100;
Console.WriteLine(number);
Console.ReadKey();//输出100
int number;
number = 100;
Console.WriteLine("number");
Console.ReadKey();//输出的是number

5.变量的命名规范

5.1注意事项

int number ;
int number = 20;

5.2命名规范

6.赋值运算符

int n = 100;//把100赋给变量n;
int n = 20;
n = 50;
Console.WriteLine(n);
Console.RadKey();

7.+的使用

Console.WriteLine(5+5);
Console.ReadKey();
Console.WriteLine(5+"5");
Console.ReadKey();

8.两个练习

在这里插入图片描述

string name = "卡卡西";
int age = 30;
string email = "kakaxi@qq.com";
string address = "火影村";
decimal salary = 5000m;
Console.WriteLine("我叫"+name+",我今年"+age+"岁了,我住在"+address+",我的邮箱是"+email+",我的工资是"+salary);
Console.ReadKey();
int age = 18;
age = 81;
Console.WriteLine(age);
Cosole.ReadKey();

9.占位符的使用

Console.WriteLine("我叫"+name+",我今年"+age+"岁了,我住在"+address+",我的邮箱是"+email+",我的工资是"+salary);
string name = "卡卡西";
string address = "火影村";
int age = 30;
string email = "kakaxi@qq.com";
decimal salary = 5000m;
Cosole.WriteLine("我叫{0},我今年{1}岁了,我住在{2},我的邮箱是{3},我的工资是{4}",name,age,address,email,salary);
Console.ReadKey();

9.1注意事项

9.2输出顺序

int n1 = 10;
int n2 = 20;
int n3 = 30;
Console.WriteLine("第一个数字是:{0},第二个数字是:{1},第三个数字是:{2}",n1,n2,n3);
Console.ReadKey(); 
int n1 = 10;
int n2 = 20;
int n3 = 30;
Console.WriteLine("第一个数字是:{1},第二个数字是{0},第三个数字是:{2}",n1,n2,n3);
Console.ReadKey();

10.交换变量

10.1增加一个中间变量

            int n1 = 10;
            int n2 = 20;
            int temp = n1;
            n1 = n2;
            n2 = temp;
            Console.WriteLine("交换后的n1={0},n2={1}", n1, n2);
            Console.ReadKey();

10.2 根据两个数凑出来你需要的值

int n1 = 10;
int n2 = 20;
n1= n1-n2;//这时n1=-10,n2=20
n2 = n2+n1;//这时n1=-10;n2=10
n1=n2-n1;//这时n1=20;n2=10
Console.WriteLine("交换后的n1={0},n2={1}",n1,n2);
Console.ReadKey();

11.接受用户输入

Cosole.ReadLine();
Console.WriteLine("请输入您的姓名:");
string name = Console.ReadLine();
Console.WriteLine("您的姓名为:{0}",name);
Console.ReadKey();

注意:这里的Console.ReadLine();的接收类型必须是个字符串类型,其他的不可以。
练习1:
在这里插入图片描述

            Console.WriteLine("您的姓名是:{0}", name);
            Console.ReadKey();
            Console.WriteLine("请问您喜欢吃什么水果?");
            string fruit = Console.ReadLine();
            Console.WriteLine("这么巧,我也喜欢吃{0}", fruit);
            Console.ReadKey();

在这里插入图片描述

 Console.WriteLine("请问您的姓名是:");
            string name = Console.ReadLine();
            Console.WriteLine("请问您的年龄是:");
            string age = Console.ReadLine();
            Console.WriteLine("请问您的性别是:");
            string gender = Console.ReadLine();
            Console.WriteLine("您好,您的姓名是{0},您的年龄是{1},您的性别是{2}", name, age, gender);
            Console.ReadKey();

12.转义字符

12.1转义字符的含义

12.2常见的转义字符

13.算术运算符

int n1 = 10;
int n2 = 3;
Console.WriteLine(n1/n2);
Console.ReadKey();

13.1练习


            int r = 5;
            double area = r * 3.14 * r;
            double perimeter = r * 3.14 * 2;
            Console.WriteLine("圆的面积:{0},圆的周长:{1}",area,perimeter);
            Console.ReadKey();
 int T_shirt = 35;
 int trousers = 120;
 int  price = T_shirt * 3 + trousers * 2;
 Console.WriteLine("小明应该付:{0}",price*0.88);
 Console.ReadLine();

14.类型转换

14.1隐式类型转换

14.2显式类型转换

 double a = 30.65;
 int b= (int)a;
 Console.WriteLine(b);
 Console.ReadKey();
            int a = 10;
            int b = 3;

            double c = a / b;
            Console.WriteLine(c);
            Console.ReadKey();

下面将一个操作数变成double类型(将一个操作数变成double类型的简单方法是*1.0)

            int a = 10;
            int b = 3;

            double c = a / b;
            Console.WriteLine(c);
            Console.ReadKey();

或者,可以直接将操作数改为double类型


            double a = 10;
            int b = 3;

            double c = a / b;
            Console.WriteLine(c);
            Console.ReadKey();

14.3 保留小数位方法

首先用占位符,其次保留几位占位符写小数点后几位,例子如下:

            double a = 10;
            int b = 3;

            double c = a / b;
            Console.WriteLine("{0:0.00}",c);
            Console.ReadKey();

15.总结

在这里插入图片描述

举报

相关推荐

0 条评论