您的位置:首页 > 其它

.Net基本语法学习(1)

2016-07-14 23:14 387 查看
1、占位符按照挖坑的顺序输出。

占位符的使用:

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: