.Net基本语法学习(1)

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;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值