占位符
使用方法:先挖个坑,再填个坑。
使用占位符需要注意的地方:
-
挖了几个坑就填几个坑,多填没效果,少填异常
异常——语法上并没有错误,只不过在程序运行的时候,由于某些原因出现了问题,使程序不能正常运行
-
输出顺序:占位符按照挖坑的顺序输出
/*string name = "刘";
string gender = "男";
int age = 22;
long telephone = 18770000000;
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我的电话号码是{3}", name, age, gender, telephone);
练习1——交换变量
//int n4 = 10;
//int n5 = 20;
//int temp = n4;
//n4 = n5;
//n5= temp;
int n4 = 10;
int n5 = 20;
n5 = n5 - n4;
n4 = n5 + n4;
n5 = n4 - n5;
Console.WriteLine("交换后,n4的值是{0},n5的值是{1}", n4, n5);
Console.ReadKey();
练习2——接收用户的输入
Console.WriteLine("您的姓名是");
string str1 = Console.ReadLine();
Console.WriteLine("您的性别是");
string str2 = Console.ReadLine();
Console.WriteLine("您的年龄是");
string str3 = Console.ReadLine();
Console.WriteLine("{0}您好,您的年龄是{2},性别{1}",str1,str2,str3);
Console.ReadLine();
转义符
转义符就是“\”+一个特殊的字符,组成了一个具有特殊意义的 字符
\n:表示换行(苹果系统)
\r\n:表示换行(Windows系统)
\ +英文半角:表示一个英文半角符
\t:表示一个tab键的空格
\b:表示一个退格键,放到字符串的两边没有效果
\\:表示一个\符
@:
- 取消\在字符串中的转义作用,单纯表示\
- 将字符串按照编辑的原格式输出
算数运算符
隐式类型转换:我们要求等号两边参与运算的操作数的类型必须一致,如果不一致,满足下列条件会发生自动类型转换,或者称之为隐式类型转化
- 两种类型兼容
例如:int和double兼容(都是数字类型)
- 目标类型大于源类型
例如:double>int (小的转大的)
显式类型转换:
- 两种类型兼容
- 大的转成小的
语法:
(待转换的类型)要转换的值;
保留小数的制定位数:
保留两位小数——{0:0.00}
练习3——算数运算
//int r = 5;
//double area=3.14*r*r;
//double perimeter = 2 * 3.14 * r;
//Console.WriteLine("圆的面积是{0},周长是{1}",area,perimeter);
//Console.ReadKey();
//int price_of_T_shirt = 35;
//int price_of_trousers = 120;
//Console.WriteLine(3*price_of_T_shirt+2*price_of_trousers);
//Console.ReadKey();
int n1 = 10;
double n2=3;
Console.WriteLine("{0:0.00}",n1/n2);
Console.ReadKey();
//int date = 46;
//int week = date / 7;
//int day = date % 7;
//Console.WriteLine("{0}周零{1}天",week,day);
//Console.ReadKey();
//Console.WriteLine("现在是几小时?");
//string hour = Console.ReadLine();
//Console.WriteLine("现在是多少分钟?");
//string minute = Console.ReadLine();
//Console.WriteLine("现在是几秒?");
//string seconds = Console.ReadLine();
//Console.WriteLine("现在是{0}小时{1}分钟{2}秒", hour, minute, seconds);
//Console.ReadLine();
int second = 107653;
int day = second / (24 * 3600);
int hours = second % (24 * 3600) / 3600;
int minute = second % (24 * 3600) % 3600 / 60;
int seconds = second % (24 * 3600) % 3600 % 60;
Console.WriteLine("{0}秒是{1}天{2}小时{3}分钟{4}秒", second, day, hours, minute, seconds);
Console.ReadKey();