.Net基本语法学习(1)
2016-07-14 23:14
387 查看
1、占位符按照挖坑的顺序输出。
占位符的使用:
2、交换两个int类型的变量:
(1)方法一、
(2)方法二、
3、接收用户输入:
Console.WriteLine("请输入你的姓名:");
string name = Console.ReadLine();
Console.WriteLine("你的姓名是{0}", name);
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();4、转义符和@符号的作用
转义符:换行\n,制表符\t
@保留原格式输出
1>两种类型兼容
2>目标类型大于源类型
强制类型转换,就是显式类型转换
1>两种类型兼容
2>大的转成小的
语法:
{待转换的类型}要转换的值;
占位符的使用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace zhanweifu { class Program { static void Main(string[] args) { int n1 = 10; int n2 = 20; int n3 = 30; Console.WriteLine("第一个数字是{0},第二个数字是{1},第三个数字是{2}", n1, n2, n3); Console.ReadKey(); } } }
2、交换两个int类型的变量:
(1)方法一、
int n1 = 10; int n2 = 20; int temp = n1; n1 = n2; n2 = temp;
(2)方法二、
int n1 = 10; int n2 = 20; n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2;(3)方法三、
int n1 = 50; int n2 = 30; n1 = n1 - n2; n2 = n1 + n2; n1 = n2 - n1;
3、接收用户输入:
Console.WriteLine("请输入你的姓名:");
string name = Console.ReadLine();
Console.WriteLine("你的姓名是{0}", name);
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();4、转义符和@符号的作用
转义符:换行\n,制表符\t
Console.WriteLine("我爱你"); Console.WriteLine("我"+"爱你"); Console.WriteLine("我\"\"爱你");
Console.WriteLine("{0}\t\t\t{1}", n1, n2); Console.WriteLine("{0}\t{1}",n1,n2);
\b表示一个delete/退格键,在两端的不起作用。
Console.WriteLine("\b学习有用\b吗?\b");@写入文件
string str = "今天天气好晴朗\r\n处处好风光"; System.IO.File.WriteAllText(@"C:\111.txt", str); Console.WriteLine("写入文件成功!"); Console.ReadKey();\\也是一个转自字符,表示\
string path = "F:\\a\\b\\c\\te.avi"; Console.WriteLine(path); Console.ReadKey();@取消斜线在字符串中的转义作用,单纯的表示斜线。
string path = @"F:\a\b\c\te.avi"; Console.WriteLine(path); Console.ReadKey();
@保留原格式输出
Console.WriteLine(@"今天天气 很不错"); Console.ReadKey();自动类型转换,就是隐式类型转换
1>两种类型兼容
2>目标类型大于源类型
int number = 10; //int--double double d = number;
强制类型转换,就是显式类型转换
1>两种类型兼容
2>大的转成小的
语法:
{待转换的类型}要转换的值;
double d = 303.6; int n = (int)d;
相关文章推荐
- 373. Find K Pairs with Smallest Sums
- VMware配置nginx之后,无法再本地访问
- VNC远程登录树莓派的图形界面
- html5离线缓存使用
- PowerDesigner数据库建模
- JavaEE学习路线图
- Elipse中如何快速安装SVN?
- CVPR2012论文
- 多进程zmq通信项目 MultiProCom + 期货基础知识
- iOS 图片填充 UIImageView
- 安装oracle11.2.0.4提示缺少elfutils-libelf-devel-0.97包
- 兼容IE8问题
- kali信息搜集工具之dnmap
- u3d honey hex framework 代码解读记录(四)
- 2013CVPR论文
- 371. Sum of Two Integers
- 使用FrameLayout替换Fragment时 不销毁Fragment
- C++设计模式实例图解
- Windows下wmic获取计算机硬件信息
- Singleton