zoukankan      html  css  js  c++  java
  • c#基础知识

    1.交换两个整形变量,不允许声明第三个变量

    1             int n1 = 11;
    2             int n2 = 20;
    3             Console.WriteLine(n1 );
    4             Console.WriteLine(n2);
    5             n1 = n2 - n1;
    6             n2 = n2 - n1;
    7             n1 = n2 + n1;
    8             Console.WriteLine(n1 );
    9             Console.WriteLine(n2);

    2.声明一个字符串类型的变量用来接收用户输入的变量

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 转义符
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("请输入你的姓名:");
    14             string name = Console.ReadLine();
    15             Console.WriteLine("您的姓名是{0}",name );
    16         }
    17     }
    18 }

    3.字符串中的转义符

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 转义符
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("今天天气好晴朗,处处好风光!");
    14             Console.WriteLine("今天天气好晴朗,
    处处好风光!");//  
    表示换行
    15 
    16             Console.WriteLine("在这句话中加入一个"英文半角的双引号!");//  "  左斜线表示转义,是个 英文半角的双引号
    17 
    18             string name1 = "张三";
    19             string name2 = "李四";
    20             string name3 = "王五";
    21             string name4 = "赵六";
    22             Console.WriteLine("{0}	{1}", name1, name2);//	 表示一个制表符
    23             Console.WriteLine("{0}	{1}", name3, name4);
    24             Console.WriteLine(name4 + "");// 表示一个退格,放在字符串的两端没有用处
    25             Console.WriteLine("zhangsan");//  放在字符串的中间可以倒着删除一个字符
    26             Console.WriteLine("我喜欢大
    狗狗");  // 
    在windows系统文本文档中可以换行,在控制台也可以换行
    27             string str1 = "今天天气好晴朗,
    处处好风光";
    28             //我们不想每个都写两个左斜杠,那我们在双引号前加@ 取消在字符串中的转义作用,使其单纯的表示
    29             System.IO.File.WriteAllText(@"E:1.txt", str1);
    30             string path = "E:\a\b\c\1.txt";  //输出 E:ac1.txt 可见\表示
    31             //@还表示按原格式输出
    32             Console.WriteLine(@"今天天气好晴朗
    33        处处好风光!");
    34             Console.WriteLine(path);
    35             Console.WriteLine("ok");
    36             Console.ReadKey();
    37         }
    38     }
    39 }

    4.convert转换

    1             string str = "123";
    2             int num1 = Convert.ToInt32(str);
    3             Console.WriteLine(num1);
    4             double num2;
    5             num2 = Convert.ToDouble(str);
    6             Console.WriteLine(num2 );

    5.关于解决方案中有多个项目的时候,若新建的项目(就是在解决方案上右键新建项目)感觉名字起的不好了,想重命名,会发现里面的命名空间并没有随重命名改变,所以一定删掉重建,因为当两个项目有交集的时候不会出现麻烦。     另外项目的名字不要用关键字,用了的话在项目中关键字就不能用了,非想用的话可以前面加个序号。

    6.判断闰年

    1             //判断闰年
    2             //年份能被400整除或 年份能够被4整除但不能被100整除
    3             Console.WriteLine("判断年份是否是闰年,请输入一个年份:");
    4             int year = Convert.ToInt32(Console.ReadLine());
    5             bool b = year % 400 == 0 || year % 4 == 0 && year % 100 != 0;//当一个表达式中同时存在 || 和&&时,与的优先级高于或,当然可以加括号,即好读又不乱。我们不用|和&了,因为效率稍低些
    6             Console.WriteLine(b);

    7.分支结构

     1              /*分支结构: if if-else
     2              * 选择结构:if else-if  switch-case
     3              * 循环结构: while  do-while for foreach
     4              */
     5             //if (判断条件)
     6             //{
     7             //    要执行的代码;
     8             //}
     9             //先判断后执行,可能一行代码都不执行
    10             Console.WriteLine("请输入你跪键盘的时间:");
    11             int mins = Convert.ToInt32(Console .ReadLine ());
    12             if (mins>60)
    13             {
    14                 Console.WriteLine("好老公,起来吧,去吃屎吧!");
    15             }
    16             Console.ReadKey();
     1             //if-else 最少执行一个,用于两种情况的判断
     2             //if (true)
     3             //{
     4             //    ;//要执行的代码
     5             //}
     6             //else
     7             //{
     8             //    //if的条件不满足,则执行这儿的语句
     9             //}
    10             //if else-if,多条件区间性;;;;;如果if成立,则执行if的语句,然后中断下面的判断,只要有if成立则执行并跳出,否则继续向下执行,最后若有else则执行,没有else则不执行
    11             Console.WriteLine("请输入你的成绩:");
    12             double score = Convert.ToInt32(Console.ReadLine ());
    13 
    14             if (score >=90)
    15             {
    16                 Console.WriteLine( "A");
    17             }
    18             else if (score >=80)
    19             {
    20                 Console.WriteLine("B");
    21             }
    22             else if (score >=70)
    23             {
    24                 Console.WriteLine("C");
    25             }
    26             else if (score >= 60)
    27             {
    28                 Console.WriteLine("D");
    29             }
    30             //else
    31             //{
    32             //    Console.WriteLine("E");    
    33             //}
    34             else if (score < 60)//这样写也行
    35             {
    36                 Console.WriteLine("E");    
    37             }

    8. 异常的捕获

     1             //抛异常
     2             Console.WriteLine("请输入一个整数:");
     3             try
     4             {
     5                 int num = Convert.ToInt32(Console.ReadLine());
     6                 Console.WriteLine("您输入的整数为:{0}", num);
     7             }
     8                 //记得try和catch之间不能有任何代码
     9             catch 
    10             {
    11                 Console.WriteLine("您输入的不是整数,不能转换为int类型的整数!");    
    12             }
  • 相关阅读:
    架构的上层是系统,是系统要素的组织形式
    计数与方法论、哲学
    网络编程--会话层、表示层、应用层
    面向中间件编程--草稿
    泛型:基于类型组合的算法和结构构建---数据结构与算法
    面向对象:消息机制更适合描述;
    类型的连接:实连接、虚连接
    数据库 = filesystem + transcation + dsl + dslengine
    一文看透浏览器架构
    代码的结合性:继承 扩展 组合 变换--swift暗含的四根主线
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/5848462.html
Copyright © 2011-2022 走看看