csharp基础


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 * 这是基于.net4.0开发的C#语言基础教程
 * author:zy
 * date:
 * 
 */ 
namespace first_lesson_1
{
    class Program
    {
       /// <summary>
       /// 这是一段入口函数的代码(表示的是程序从此处开始运行)
       /// </summary>
       /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World,C#");//输出一段信息到控制台中显示
            Console.ReadKey();//等待用户的输入信息,防止屏幕一闪而过
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace four_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //变量定义的格式 :数据类型 变量名 = 赋值;
            int a = 12;
            float b = 15.3f;

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace five_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义一本书的售价为20.12元
            float fltPrice = 20.12f;//匈牙利命名规则
            /*
             * b.一辆小汽车的乘坐人数为5人。

               c.输出信息为“大家来学C#”

                d.表示灯泡的开和关的状态。
             */
            int intSeatNum = 5;//Pascal命名规则
            

            string strInfoName = "大家来学C#";//string是引用类型

            bool boolIfLight = true;//值类型

           
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace six_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * 装箱的操作:是把值类型转换成引用类型,可以采取隐式转换
             */
            int intNum = 12;//值类型,它是在栈中存储
            //这里会涉及到一个类型的转换,需要把值类型转换成引用类型
            object objNum =intNum;//将值类型转换成引用类型(隐式转换)

            //把引用类型转换成值类型,这种过程称之为拆箱。必须是显示转换
            //引用类型,现在需要将引用类型转换成值类型
            object objA = 15;
            int intGetObjA =(int) objA;
            int intGetObjA1 = Convert.ToInt32(objA);//强制类型的转换。

            object objB = 12.3;
            float fltB = (float)objB;
            float fltB1 = Convert.ToSingle(objB);//把引用类型转成单精度的浮点数据类型

            object objC = 145.6;
            double dblC = (double)objC;
            double dblC1 = Convert.ToDouble(objC);//把引用类型转换成双精度的数据类型




        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace seven_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //float fltA = 12.3F;//在数值后面加上一个符号就明确它的数据类型
            //double dblA = 12.3;
            //int intA = 0x10;
            //long longA = 10L;
            //uint uintA = 10u;
            //Console.WriteLine(intA);//向屏幕输出数据,而这个数据就是括号里的数据
            //转义,
            //char charA = '\'';
            //Console.WriteLine(charA);
            //string strInfo = "这是第一行,\n这是第二行";
            //Console.WriteLine(strInfo);
            //string strBookName ="这是一本\"C#编程的书籍\"";
            //Console.WriteLine(strBookName);
            //const关键字表示的是常量,也就是说常量的值是无法改变的。
            const double dblPi = 3.14;
            int intConstA = 20;
            intConstA = 30;

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eight_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //int a=20, b=10,c;
            //c = a + b;
            //int d = 27, e = 5;
            //int f = d % e;
            //Console.WriteLine(f);

            //int h = 20, m = 19;
            ////比较运算符,它的运算结果就只能为bool类型。true或者false的结果
            //bool boolResult = h != m;
            //Console.WriteLine(boolResult);

            //一元运算符
          //  int x = 20, y = 15;
          //   x *= y;
          ////  x = x * y;
          //  Console.WriteLine(x);
            int j = 1;
            //j++;//j =j+1;
            j--;//j=j-1;
            Console.WriteLine(j);


        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nine_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            ////bool c = (12 + 13) * 4 >= 27;
            //int a = 10;
            //double b = 10.1;
            //double c = a + b;
            ////如果想计算两个整型的数据,运算结果我们要求有小数点
            //int m = 10, h = 4;
            //double dresult = m*1.0 / h;
            //Console.WriteLine(dresult);
            //int? i = null;
            //var cc = 12 + 13;
            //var mm = "hello";
            //var ff = 12 *1.0f/ 3;
            Console.WriteLine("请输出一个数字");
           int a = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("请输出另一个数字");
           int b= Convert.ToInt32(Console.ReadLine());
           Console.WriteLine(a + b);

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ten_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入年龄:");
            string strAge = Console.ReadLine();//接收用户的输入
            int intAge = Convert.ToInt32(strAge);
            //如果18以下未成年,18到35青年,36-60中年,61之后就是老年。
            //选择语句
            //if (intAge >= 18)//if括号里如果表达式的运算结果为true,那么执行if花括号里的语句
            //{
            //    Console.WriteLine("用户已经成年");
            //}
            //else
            //{
            //    Console.WriteLine("用户未成年");
            //}
            if (intAge < 18)
            {
                Console.WriteLine("用户未成年");
            }
            else if (intAge >= 18 && intAge <= 35)
            {
                Console.WriteLine("用户是青年");
            }
            else if (intAge >= 36 && intAge <= 60)
            {
                Console.WriteLine("用户中年");
            }
            else //if (intAge > 60)
            {
                Console.WriteLine("用户是老年");
            }

            Console.ReadKey();//等待用户的输入,避免屏幕一闪而过。
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eleven_lesson_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //接收用户的输入
            Console.WriteLine("请输入月份,1-3表示春天,4-6夏天,7-9秋天,10-12冬天");
            string strInput = Console.ReadLine();
            switch (strInput)
            {
                case "1":                   
                case "2":
                case "3":
                    Console.WriteLine("春天");
                    break;
                case "7":     
                case "8":                
                case "9":
                    Console.WriteLine("秋天");
                    break;//每个case代表一个分支。
                case "10":
                case "11":
                case "12":
                    Console.WriteLine("冬天");
                    break;
                case "4":
                case "5":
                case "6":
                    Console.WriteLine("夏天");
                    Console.WriteLine("---------");
                    break;
                default://如果case都不匹配的情况下,程序的执行代码
                    Console.WriteLine("你输入不符合要求");
                    break;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lesson_12_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //int i = 1;
            //int sum = 0;
            ////while循环,要执行循环体的语句,就先判断while后面的逻辑表达式的值,当值为真得时候,才会执行
            //while (i <= 100)
            //{
            //    sum = sum + i;//sum+=i;
            //    i++;//i自增1
            //}
            //Console.WriteLine(sum);
            double intBen = 5000;
            int count = 0;
            while (intBen < 10000)
            {
                intBen = intBen * 1.0414;
                Console.WriteLine(intBen);
                count++;
            }
           
            Console.WriteLine(count);


        }
    }
}

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !