C#自学笔记_002_Real

C#自学笔记_002_Real

C#基础性知识

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Csharp002
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //类型转换:
            //double-->>int 隐式类型转换    int-->>double 显式类型转换
            //string-->double  需要用Convert进行转换
            string s1 = "123";
            double d1 = Convert.ToDouble(s1); //用Convert进行转换,需要定义相应的变量去接收相应的数值
            int n1 = Convert.ToInt32(s1); //转换为int类型选择 ToInt32
            Console.WriteLine(d1 + 12);
            Console.WriteLine(n1 + 10);
            
            //练习:用户输入姓名、三门课成绩、求出总成绩与平均成绩
            Console.WriteLine("请输入您的姓名:");
            string name1 = Console.ReadLine();
            Console.WriteLine("请输入您的语文成绩:");
            string yuwen = Console.ReadLine();
            Console.WriteLine("请输入您的数学成绩:");
            string shuxue = Console.ReadLine();
            Console.WriteLine("请输入您的英语成绩:");
            string yingyu = Console.ReadLine();
            double a, b, c;
            a = Convert.ToDouble(yuwen);
            b = Convert.ToDouble(shuxue);
            c = Convert.ToDouble(yingyu);
            Console.WriteLine("{0},您的总成绩是{1},平均成绩是{2}。", name1, (a + b + c), (a + b + c) / 3);
            
            //++运算符与--运算符
            //对于++和--运算,只需要一个操作数这样的运算符称为一元运算符
            //对于+ - * /这样的需要两个操作数的运算符称为二元运算符
            //在程序运算中一元运算符的优先级高于二元运算符
            int a2 = 5;
            int b2 = a2++ + ++a2 * 2 + --a2 + ++a2;
            Console.WriteLine(a2);
            Console.WriteLine(b2);

            //关系运算符,bool运算 < > <= >= == !=
            //布尔运算表达式得到的值应该赋值给布尔类型变量
            bool b3 = 1500 > 1;
            bool b4 = 1 > 1500;
            Console.WriteLine("b3 = {0}", b3);//输出b3 = True
            Console.WriteLine("b4 = {0}", b4);//输出b4 = False

            //逻辑运算符  ||或  &&与  !非(略)

            //复合赋值运算符 += -= *= /= %= (略)

            //练习:输入一个年份判断是否为闰年
            //年份能够被400整除
            //年份能够被4整除,但是不能被100整除
            Console.WriteLine("请输入您要查询的年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            bool b5 = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
            Console.WriteLine("b5 = {0}", b5);

            //if语句练习
            //练习一:用户输入账号名与密码,如果账号名为admin,密码为password,则提示登录成功
            Console.WriteLine("请输入您的账号名:");
            string name3 = Console.ReadLine();
            Console.WriteLine("请输入您的密码:");
            string password = Console.ReadLine();
            if (name3 == "admin" && password == "password")
            {
                Console.WriteLine("登陆成功!!");
            }
            else 
            {
                Console.WriteLine("登录失败,用户名或者密码错误!");
            }
            //练习二,判断年份是否为闰年
            //年份能够被400整除
            //年份能够被4整除,但是不能被100整除
            Console.WriteLine("请输入您要查询的年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            bool b5 = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
            if (b5)
            {
                Console.WriteLine("{0}是闰年", year);
            }
            else
            {
                Console.WriteLine("{0}不是闰年", year);
            }            

            //if-elseif-else语句练习
            //输入考试成绩,90-100-->A 80-90-->B  70-80-->C  60-70-->D  0-60-->E
            Console.WriteLine("请输入你的考试成绩:");
            int score = Convert.ToInt32(Console.ReadLine());
            if (0 > score || score > 100)
            {
                Console.WriteLine("输入有误,请输入有效成绩");
                Console.ReadKey();
            }
            else 
            {
                if (90 <= score && score <= 100)
                {
                    Console.WriteLine("你的成绩等级为A级。");
                }
                else if (80 <= score && score < 90)
                {
                    Console.WriteLine("您的成绩等级为B级。");
                }
                else if (70 <= score && score < 80)
                {
                    Console.WriteLine("您的成绩等级为C级。");
                }
                else if (60 <= score && score < 70)
                {
                    Console.WriteLine("您的成绩等级为D级。");
                }
                else
                {
                    Console.WriteLine("您的成绩等级为E级。");
                }
                
            }          

            //重要知识点:异常捕获
            //我们在程序中经常会出现各种各样的异常,你如果想要你的程序变得更坚强一些
            //在你的代码中应该尽量地使用try-catch语句来进行异常捕捉
            //语法结构:
            //try
            //{
            //    可能出现异常的代码;
            //    ...;
            //    ...;
            //}
            //catch
            //{
            //    出现异常后要执行的代码
            //}
            //执行过程:如果try中的代码没有出现异常,那么catch里的代码将不执行,
            //如果try语句中的代码出现异常,后面的所有代码将不再执行,直接执行catch里的代码
            //try语句与catch语句张健不能写代码,必须紧密相连
            //例子练习:输入一个数字,在屏幕上打印出它的两倍
            Console.WriteLine("请输入一个数字:");
            bool b6 = true;  //让代码满足一些条件才能去执行的话,在工程中最常用bool类型(需要学习的点)
            double num6 = 0; //变量num6必须在try语句外进行定义并进行赋值操作,否则会报错
            try
            {
                num6 = Convert.ToDouble(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("您输入的数字有误,不能进行正常运算!!");
                b6 = false;
            }
            if(b6)
            {
                Console.WriteLine("num6 * 2 = {0}", num6 * 2);
            }

            //switch--case语句练习
            //李四的年终工作评定,A级工资涨500元,B级工资涨200元,C级工资不变,D级工资降200元,E级工资降500元
            double salary = 5000;
            Console.WriteLine("请输入李四的工作评级:\n等级为A、B、C、D、E");
            string level = Console.ReadLine();
            bool b7 = true;
            switch(level)
            {
                case "A":
                    salary += 500;
                    break;
                case "B":
                    salary += 200;
                    break;
                case "C":
                    salary += 0;
                    break;
                case "D":
                    salary -= 200;
                    break;
                case "E":
                    salary -= 500;
                    break;
                default:
                    Console.WriteLine("请输入正确的等级。");
                    b7 = false;
                    break;
            }
            if(b7)
            {
                Console.WriteLine("李四明年的月薪为:{0}", salary);
            }            

            //while循环语句
            //练习一,求1到100之间所有整数的和
            int i = 0;
            int sum1 = 0;
            while (i < +100)
            {
                sum1 += i;
                i++;
            }
            Console.WriteLine("1到100之间所有证书的和为: {0}", sum1);
            //练习二:用户登录,账号密码不对就一直让输入,admin   888888
            string name = "";
            string password = "";
            while (true)
            {
                if (name != "admin" || password != "888888")
                {
                    Console.WriteLine("请输入您的账号:");
                    name = Console.ReadLine();
                    Console.WriteLine("请输入您的密码:");
                    password = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("输入正确,登陆成功!!");
                    break;
                }
            }
            //练习三,输入班级人数,输入每个人的成绩,计算总成绩与平均成绩
            int person, score, i;
            double avg, sum;
            sum = 0;
            i = 0;
            Console.WriteLine("请输入班级总人数:");
            try
            {
                person = Convert.ToInt32(Console.ReadLine());
                while (i < person)
                {
                    Console.WriteLine("请输入第{0}位同学的成绩", i + 1);
                    try
                    {
                        score = Convert.ToInt32(Console.ReadLine());
                        sum += score;
                        i++;
                    }
                    catch
                    {
                        Console.WriteLine("输入的成绩有误");
                    }
                }
                avg = sum / person;
                Console.WriteLine("班级共有{0}人,总成绩为{1}分,平均成绩为{2:0.00}分。", person, sum, avg);
            }
            catch 
            {
                Console.WriteLine("输入的班级人数有误");
            }

            //do-while语句,需要先执行一遍程序,在带着结果去进行循环的情况(略)

            //continue关键字:立即结束本次循环,回到判断条件进行判断,如何符合条件,则进入下一循环,
            //如果不符合条件,则直接跳出本循环语句,执行下面的语句
            //也是与if选择语句一块用
            while(true)
            {
                Console.WriteLine("Hello Wolrd!!");
                break;  //只循环一次
                //continue;  //一直循环不停止
            }
            //练习-:用while与continue实现从1到100(含)之间除了能够被7整除的数之外其他所有数的和
            int i = 1;
            int sum8 = 0;
            while (i <= 100)
            {
                
                if (i % 7 == 0)
                {
                    i++;
                    continue;
                }
                sum8 += i;
                i++;
            }
            Console.WriteLine("sum8 = {0}", sum8);


            //for循环语句  //forr逆向for循环!!!
            //练习一:找到从1到100内粉所有指数(素数)
            for (int i = 2; i <= 100; i++)
            {
                bool b = true;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        b = false;
                        break;
                    }
                }
                if (b)
                {
                    Console.WriteLine(i);
                }

            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仲子_real

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值