C#第三天

116 阅读12分钟

image.png

1、类型如果相兼容的两个变量,可以使用自动类型转换或者强制类型转换, 但是,如果两个类型的变量不兼容,比如 string与int或者string 与double, 这个时候我们可以使用一个叫做Convert的转换工厂进行转换。 注意:使用Convert进行类型转换,也需要满足一个条件: 面儿上必须要过的去。

2、算数运算符 ++:分为前++和后++,不管是前++还是后++,最终的结果都是给这个变量加一。 区别表现表达式当中,如果是前++,则先给这个变量自身加一,然后带着这个加一后的值去参与运算。 如果是后++,则先拿原值参与运算,运算完成后,再讲这个变量自身加一。 --:同上。

3、 对于向加加或者减减这样只需要一个操作数就能完成的运算,我们称之为一元运算符。 + - * / % 对于这些需要两个或以上才能完成运算的操作符,我们称之为二元运算符。 一元运算符的优先级要高于而元运算符。 如果在一个表达式当中,既有一元运算符,又有二元运算符,我们首先计算一元运算符。

int number=10; int result=10 + ++number;

4、关系运算符 > < >= <= == != 关系运算符是用来描述两个事物之间的关系 由关系运算符连接的表达式称之为关系表达式。

5、bool类型 在c#中我们用bool类型来描述对或者错。 bool类型的值只有两个 一个true  一个false

6、逻辑运算符 && 逻辑与 ||逻辑或 !逻辑非 又逻辑运算符连接的表达式叫做逻辑表达式

逻辑运算符两边放的一般都是关系表达式或者bool类型的值。  5>3 &&true    3>5||false    !表达式  逻辑表达式的结果同样也是bool类 

7、复合赋值运算符 int number=10; += : number+=20; number=number+20; -= number-=5; number=number-5; = number=5; number=number*5; /= %=

顺序结构:程序从Main函数进入,从上到下一行一行的执行,不会落下任何一行。 分支结构:if  if-else 选择结构:if else-if switch-case 循环结构:while do-while for foreach

8、 if语句: 语法: if(判断条件) {  要执行的代码; } 判断条件:一般为关系表达式或者bool类型的值。 执行过程:程序运行到if处,首先判断if所带的小括号中的判断条件, 如果条件成立,也就是返回true,则执行if所带的大括号中的代码, 如果判断条件不成立,也就是返回一个false。则跳过if结构,继续向下执行。

if结构的特点:先判断,再执行,有可能一行代码都不执行 用于一种情况的判断。

9、if-else 语法: if(判断条件) {  执行的代码; } else {  执行的代码 } 执行过程:程序执行到if处,首先判断if所带的小括号中的判断条件是否成立, 如果成立,也就是返回一个true,则执行if所带的大括号中的代码, 执行完成后,跳出if-else结构。 如果if所带的小括号中的判断条件不成立,也就是返回一个false, 则跳过if语句,执行else所带的大括号中的语句,执行完成后,跳出if-else结构。

if-else特点:先判断,再执行,最少都要执行一条代码。 用于两种情况的判断

注意:else永远跟离它最近的那个if配对

10、if else-if 作用:用来处理多条件的区间性的判断。 语法: if(判断条件) {  要执行的代码; } else if(判断条件) {  要执行的代码; } else if(判断条件) {  要执行的代码; } else if(判断条件) {  要执行的代码; } ........ else {  要执行的代码; } 执行过程;程序首先判断第一个if所带的小括号中的判断条件,如果条件成立,也就是返回一个true, 则执行该if所带的大括号中的代码,执行完成后,立即跳出if else-if结构。 如果第一个if所带的判断条件不成立,也就是返回一个false,则继续向下进行判断,依次的判断每一个if所带 的判断条件,如果成立,就执行该if所带的大括号中的代码,如果不成立,则继续向下判断, 如果每个if所带的判断条件都不成立,就看当前这个if else-if结构中是否存在else。 如果有else的话,则执行else中所带的代码,如果没有else,则整个 if-else if神马都不做。 else可以省略。

01复习

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

namespace _01复习
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             变量
             *
             * 赋值运算符=  int num=10;
             * 占位符
             * 变量的命名规范
             * Camel:
             * Pascal:
             * +号的使用:
             * 1、连接
             * string s1="123";
             * int num=5;
             * s1+num       num+100
             * 2、相加
             * 三种注释
             * //
             * ///
             * 快捷键
             * 算数运算符
             * +=*除%
             * 转义符
             * //  /"  /r/n /b  /t
             * @ 1、取消\在字符串中的转义作用  2、按原格式输出字符串
             * 类型转换
             * 1、强制类型转换 显示类型转换
             * 2、自动类型转换  隐式类型转换
             *
             * 类型兼容 double int
             *
             *

             */
            //int num = 10;
            //double d = num;//自动 小的转大的


            //double dd = 3.13;
            //int n = (int)dd;
        }
    }
}

02、两道作业题

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

namespace _02_两道作业题
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习,编程实现计算几天(如46天)是几周零几 天.  6周零4天
            //int days = 46;
            //int weeks = days / 7;
            //int day = days % 7;
            //Console.WriteLine("{0}天是{1}周零{2}天",days,weeks,day);
            //Console.ReadKey();


            //编程实现107653秒是几天几小时几分钟几秒?


            // 60*60  3600 *24=86400   86400

            int seconds = 107653;
            int days = seconds / 86400;//求得天数
            int secs = seconds % 86400;//求得求完天数后剩余的秒数
            int hours = secs / 3600;//求得小时数
            secs = secs % 3600;//求得小时数后剩余的秒数
            int mins = secs / 60;//求得分钟数
            secs = secs % 60;

            Console.WriteLine("{0}秒是{1}天{2}小时{3}分钟{4}秒", seconds, days, hours, mins, secs);
            Console.ReadKey();
        }
    }
}

03类型转换

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

namespace _03类型转换
{
    class Program
    {
        static void Main(string[] args)
        {
            //显示类型转换、自动类型转换
            //int--double   double  ----int

            //string s = "123abc";
            ////将字符串转换成int或者double类型
            //double d = Convert.ToDouble(s);
            //int n = Convert.ToInt32(s);
            //Console.WriteLine(n);
            //Console.WriteLine(d);
            //Console.ReadKey();


            //让用户输入姓名 语文 数学 英语 三门课的成绩,
            //然后给用户显示:XX,你的总成绩为XX分,平均成绩为XX分。
            Console.WriteLine("请输入你的姓名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入你的语文成绩");
            string strChinese = Console.ReadLine();
            Console.WriteLine("请输入你的数学成绩");
            string strMath = Console.ReadLine();
            Console.WriteLine("请输入你的英语成绩");
            string strEnglish = Console.ReadLine();


            double chinese = Convert.ToDouble(strChinese);
            double math = Convert.ToDouble(strMath);
            double english = Convert.ToDouble(strEnglish);

            double sumScore = chinese + math + english;
            double avg = (int)sumScore*1.0 / 3;
            Console.WriteLine("{0}你的总成绩是{1}平均成绩是{2:0.00}", name, sumScore, avg);
            Console.ReadKey();

            //55 77  88  557788
            //由于字符串去相加的话,最终会变成相连接,如果要拿字符串类型的变量参与计算
            //需要将字符串转换成int或者double类型
            //int chinese = Convert.ToInt32(strChinese);
            //int math = Convert.ToInt32(strMath);
            //int english = Convert.ToInt32(strEnglish);

            //Console.WriteLine("{0}你的总成绩是{1},平均成绩是{2}", name, chinese + math + english, (chinese + math + english) / 3);
            //Console.ReadKey();

        }
    }
}

04Convert类型转换

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

namespace _04Convert类型转换
{
    class Program
    {
        static void Main(string[] args)
        {
            //提示用户输入一个数字 接收 并且向控制台打印用户输入的这个数字的2倍
            Console.WriteLine("请输入一个数字");
           // string strNumber = Console.ReadLine();
            //将用户输入的字符串转换成int或者double类型
            double number = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine(number*2);
            Console.ReadKey();

        }
    }
}

05加加减减

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

namespace _05加加减减
{
    class Program
    {
        static void Main(string[] args)
        {
          //  int number = 10;
          // // number++;
          ////  ++number;//number=number+1;
          // // number--;
          //  --number;
          //  Console.WriteLine(number);
          //  Console.ReadKey();

         //   int number = 10;
         ////   int result = 10 + number++;
         //   //int result = 10 + number;
         //   //number++;



         //   //int result = 10 + ++number;
         //   number++;
         //   int result = 10 + number;
         //   Console.WriteLine(number);
         //   Console.WriteLine(result);
         //   Console.ReadKey();


          //  int number = 10;
          ////  int result = 10 + number--;
          //  //int result = 10 + number;
          //  //number--;

          // // int result = 10 + --number;
          //  number--;
          //  int result = 10 + number;
          //  Console.WriteLine(number);
          //  Console.WriteLine(result);
          //  Console.ReadKey();

           // int a = 5;
           // a++;
           // ++a;
           // --a;
           // a--;
           // int b = a++ + ++a * 2 + --a + a++;
           //// int b = a + a * 2 + a + a;
           // //      5+7*2+6+6   7
           // Console.WriteLine(a);
           // Console.WriteLine(b);
           // Console.ReadKey();

            //如果你有了一个喜欢的女生,你应该好好学习,努力上个好大学,毕业找个高新的工作。
            //在你喜欢的女生结婚的时候 多随点份子钱。

        }
    }
}

06关系运算符

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

namespace _06关系运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            //            大象的重量(1500)>老鼠的重量(1)
            //关系表达式的结果是bool类型
           // bool b = 1500 > 1;
          //  bool b = 3 > 1000;
           // bool b = 39 < 18;
            bool b = 20 == 20;
            Console.WriteLine(b);
            Console.ReadKey();
            //兔子的寿命(3)>乌龟的寿命(1000)
            //39<18
            //我的年龄(20)==你的年龄(20)

        }
    }
}

07逻辑运算符练习

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

namespace _07逻辑运算符练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //让用户输入老苏的语文和数学成绩,输出以下判断是否正确,正确输出True,错误输出False
            //1)老苏的语文和数学成绩都大于90分
            Console.WriteLine("小苏,输入你的语文成绩");
            //string strChinese = Console.ReadLine();
            int chinese = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("小苏,请输入你的数学成绩");
            int math = Convert.ToInt32(Console.ReadLine());


            //bool b = chinese > 90 && math > 90;
            bool b = chinese > 90 || math > 90;
            Console.WriteLine(b);
            Console.ReadKey();



            //2)语文和数学有一门是大于90分的

        }
    }
}

08判断闰年

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

namespace _08判断闰年
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("请输入要判断的年份");
            //int year = Convert.ToInt32(Console.ReadLine());
            ////年份能够被400整除.(2000)
            ////年份能够被4整除但不能被100整除.(2008)


            ////逻辑与的优先级要高于逻辑或
            //bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);

            //Console.WriteLine(b);
            //Console.ReadKey();



          //  bool b = 5 < 3 && 5 > 3;

            bool b = 5 > 3 || 4 < 3;

        }
    }
}

09小复习

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

namespace _09小复习
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             变量类型:int double string char decimal  bool
             * 运算符:
             * 赋值运算符:=
             * 复合赋值运算符:+= -= *= /= %=
             * 算数运算符:+ - * / % ++ --
               关系运算符: > < >= <= == !=
             * 逻辑运算符: && ||  !
             * 类型转换:
             * 1、自动类型转换 小的转大的  int--->double
             * 2、强制类型转换 大的转小的  double-->int
             * 3、Convert
             */
        }
    }
}

10if结构

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

namespace _10if结构
{
    class Program
    {
        static void Main(string[] args)
        {
            //编程实现:如果跪键盘的时间大于60分钟,那么媳妇奖励我晚饭不用做了
            Console.WriteLine("请输入你跪键盘的时间");
            int mins = Convert.ToInt32(Console.ReadLine());

            //如果跪键盘的时间>60分钟 则不做晚饭

            bool b = mins > 60;
            //如果你想表示的含义是当b的值为true的时候去执行if中代码,
            //那么 语法上  ==true可以省略
            //但是,如果你想表示的是当b==false的时候去执行if中代码,
            //语法上 ==false不能省略
            if (mins>60)
            {
                Console.WriteLine("好老公,不用跪键盘了,去吃屎吧");
            }
            Console.ReadKey();


        }
    }
}

11if结构的3个练习

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

namespace _11if结构的3个练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //让用户输入年龄,如果输入的年龄大于23(含)岁,则给用户显示你到了结婚的年龄了.

            //Console.WriteLine("请输入你的年龄");
            //int age = Convert.ToInt32(Console.ReadLine());
            //bool b = age >= 23;
            //if (b)
            //{
            //    Console.WriteLine("你可以结婚啦");
            //}
            //Console.ReadKey();

            //如果老苏的(chinese  music)
            //语文成绩大于90并且音乐成绩大于80
            //语文成绩等于100并且音乐成绩大于70,则奖励100元.
            //Console.WriteLine("请输入老苏的语文成绩");
            //int chinese = Convert.ToInt32(Console.ReadLine());
            //Console.WriteLine("请输入老苏的音乐成绩");
            //int music = Convert.ToInt32(Console.ReadLine());

            //bool b = (chinese > 90 && music > 80) || (chinese == 100 && music > 70);

            //if (b)
            //{
            //    Console.WriteLine("奖励100元");
            //}
            //Console.ReadKey();

            //让用户输入用户名和密码,如果用户名为admin,密码为888888,则提示登录成功.
            Console.WriteLine("请输入用户名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string pwd = Console.ReadLine();

            if (name == "admin" && pwd == "mypass")
            {
                Console.WriteLine("登陆成功");
            }
            Console.ReadKey();


        }
    }
}

12if-else练习

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

namespace _12if_else练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //如果小赵的考试成绩大于90(含)分,那么爸爸奖励他100元钱,
            //否则的话,爸爸就让小赵跪方便面.
            //Console.WriteLine("请输入小赵的考试成绩");
            //int score = Convert.ToInt32(Console.ReadLine());
            //if (score >= 90)
            //{
            //    Console.WriteLine("奖励你一百块");
            //}
            //else
            //{
            //    Console.WriteLine("去跪方便面");
            //}
            //Console.ReadKey();

            //对学员的结业考试成绩评测
            //         成绩>=90 :A
            //90>成绩>=80 :B
            //80>成绩>=70 :C
            //70>成绩>=60 :D
            //         成绩<60   :E


            Console.WriteLine("请输入学员的考试成绩");
            int score = Convert.ToInt32(Console.ReadLine());
            //最正确的做法

            if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            }
            //else if (score < 60)
            //{
            //    Console.WriteLine("E");
            //}
            else
            {
                Console.WriteLine("E");
            }

            Console.ReadKey();




            #region if的做法
            //if (score >= 90 && score < 100)
            //{
            //    Console.WriteLine("A");
            //}
            //if (score >= 80 && score < 90)//ctrl+k+d
            //{
            //    Console.WriteLine("B");
            //}
            //if (score >= 70 && score < 80)
            //{
            //    Console.WriteLine("C");
            //}
            //if (score >= 60 && score < 70)//98  88
            //{
            //    Console.WriteLine("D");
            //}
            ////else
            ////{
            ////    Console.WriteLine("E");
            ////}
            //if (score < 60)
            //{
            //    Console.WriteLine("E");
            //}
            #endregion
            #region if else-if
            //if (score >= 90)
            //{
            //    Console.WriteLine("A");
            //}
            //else//<90
            //{
            //    if (score >= 80)
            //    {
            //        Console.WriteLine("B");
            //    }
            //    else//<80
            //    {
            //        if (score >= 70)
            //        {
            //            Console.WriteLine("C");
            //        }
            //        else//<70
            //        {
            //            if (score >= 60)
            //            {
            //                Console.WriteLine("D");
            //            }
            //            else//<60
            //            {
            //                Console.WriteLine("E");
            //            }
            //        }
            //    }
            //}
            #endregion
            Console.ReadKey();

        }
    }
}

13、练习

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

namespace _13_练习 { class Program { static void Main(string[] args) { //比较3个数字的大小 不考虑相等

        //分别的提示用户输入三个数字 我们接受并且转换成int类型
        //Console.WriteLine("请输入第一个数字");
        //int numberOne = Convert.ToInt32(Console.ReadLine());
        //Console.WriteLine("请输入第二个数字");
        //int numberTwo = Convert.ToInt32(Console.ReadLine());
        //Console.WriteLine("请输入第三个数字");
        //int numberThree = Convert.ToInt32(Console.ReadLine());


        //三种情况 应该使用 if else-if来做
        //如果第一个数字大于第二个数字 并且第一个数字还大于第三个数字
        //if (numberOne > numberTwo && numberOne > numberThree)
        //{
        //    Console.WriteLine(numberOne);
        //}
        ////如果第二个数字大于第一个数字并且第二个数字大于第三个数字
        //else if (numberTwo > numberOne && numberTwo > numberThree)
        //{
        //    Console.WriteLine(numberTwo);
        //}
        ////如果第三个数字大于第一个数字并且第三个数字大于第二个数字
        //else
        //{
        //    Console.WriteLine(numberThree);
        //}


        //我先让第一个数字跟第二个数字比较 如果大于第二个数字 再让第一个数字跟第三个数字比较
        //if (numberOne > numberTwo)
        //{
        //    //如果第一个数字大于了第二个数字 再让第一个数字跟第三个数字比较
        //    if (numberOne > numberThree)
        //    {
        //        Console.WriteLine(numberOne);
        //    }
        //    else//第三个数字要大于第一个数字
        //    {
        //        Console.WriteLine(numberThree);
        //    }
        //}
        //else//第二个数字大于了第一个数字
        //{
        //    //让第二个数字跟第三个数字进行比较 如果第二个数字大于第三个数字  第二个数字最大 否则第三个数字最大
        //    if (numberTwo > numberThree)
        //    {
        //        Console.WriteLine(numberTwo);
        //    }
        //    else//第三个数字最大
        //    {
        //        Console.WriteLine(numberThree);
        //    }
        //}



        //练习1:提示用户输入密码,如果密码是“88888”则提示正确,否则要求再输入一次,
        //如果密码是“88888”则提示正确,否则提示错误,程序结束。
        //(如果我的密码里有英文还要转换吗,密码:abc1)

        //Console.WriteLine("请输入密码");
        //string pwd = Console.ReadLine();
        //if (pwd == "888888")
        //{
        //    Console.WriteLine("登陆成功");
        //}
        //else//要求用户再输入一次
        //{
        //    Console.WriteLine("密码错误,请重新输入");
        //    pwd = Console.ReadLine();
        //    if (pwd == "888888")
        //    {
        //        Console.WriteLine("输了两遍,终于正确了");
        //    }
        //    else//输入第二次错误
        //    {
        //        Console.WriteLine("两边都不对,程序结束");
        //    }
        //}

        //Console.ReadKey();




        //练习2:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,
        //则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,
        //如果用户名是admin则提示密码错误.
        //Console.WriteLine("请输入用户名");
        //string name = Console.ReadLine();
        //Console.WriteLine("请输入密码");
        //string pwd = Console.ReadLine();


        ////第一种情况:用户名和密码全部输入正确
        //if (name == "admin" && pwd == "888888")
        //{
        //    Console.WriteLine("登陆成功");
        //}
        ////第二种情况:密码错误
        //else if (name == "admin")
        //{
        //    Console.WriteLine("密码输入错误,程序退出");
        //}
        ////第三种情况:用户名错误
        //else if (pwd == "888888")
        //{
        //    Console.WriteLine("用户名错误,程序退出");
        //}
        ////第四种情况:用户名和密码全部错误
        //else
        //{
        //    Console.WriteLine("用户名和密码全部错误,程序退出");
        //}



        //练习3:提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,
        //则告知不允许查看,如果大于等于10岁并且小于18,
        //则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,
        //否则提示"退出,你放弃查看"。

        //第一种情况  >=18
        //第二种情况  <10
        //第三种情况  >=10  && <18

        Console.WriteLine("请输入你的年龄");
        int age = Convert.ToInt32(Console.ReadLine());

        if (age >= 18)
        {
            Console.WriteLine("看吧,早晚你都要知道的");
        }
        else if (age < 10)
        {
            Console.WriteLine("滚蛋,回家吃奶去");
        }
        else
        {
            Console.WriteLine("确定要看么?yes/no");
            //input 要么是yes要么是no
            string input = Console.ReadLine();
            if (input == "yes")
            {
                Console.WriteLine("看吧,早熟的孩子,后果自负哟");
            }
            else//no
            {
                Console.WriteLine("乖孩子,回家吃奶去吧");
            }
        }

        Console.ReadKey();

    }
}

}