zoukankan      html  css  js  c++  java
  • 分支语句与练习

    1、分支语句

    1)顺序语句

    2)分支(选择)语句

    3)循环语句

    主要学习分支(选择)语句:

    //格式1 if(){}

    例子
    //int a = 116;
    //if (a <= 100 && a >= 0)
    //{
    // Console.WriteLine("这个a是100以内的数");
    //}
    //Console.WriteLine(a);
    //Console.ReadLine();

    //格式2 if(){}else{} 二选一
    //如果if满足,走if,else就不会走
    //如果if不满足,else一定会走

    例子

    //Console.Write("请输入一个整数:");
    //int a = int.Parse(Console.ReadLine());
    //if (a > 10)
    //{
    // Console.WriteLine("您输入的是一个大于10的数");
    //}
    //else
    //{
    // Console.WriteLine("您输入的数不是大于10的");
    //}

    //Console.ReadLine();

    //格式3 if(){}else if(){}...else{} 多选一
    //只要上面有一个if或者else if满足条件,执行了
    //从它以下的所有不需要再去判断读取

    例子

    //Console.Write("请输入一个月份");
    //int month = int.Parse(Console.ReadLine());
    //if (month > 2 && month < 6)
    //{
    // Console.WriteLine("您输入的是春天的月份!");
    //}
    //else if (month > 5 && month < 9)
    //{
    // Console.WriteLine("您输入的是夏天的月份!");
    //}
    //else if (month > 8 && month < 12)
    //{
    // Console.WriteLine("您输入的是秋天的月份!");
    //}
    //else if (month == 12 || month == 1 || month == 2)
    //{
    // Console.WriteLine("您输入的是冬天的月份!");
    //}
    //else
    //{
    // Console.WriteLine("输入有误!!");
    //}

    //Console.ReadLine();

    //格式4 if的嵌套
    //首先规定好大的范围,再进去判断各种小的情况

    例子

    //Console.Write("请输入一个月份");
    //int month = int.Parse(Console.ReadLine());
    //if (month >= 1 && month <= 12)
    //{
    // if (month > 2 && month < 6)
    // {
    // Console.WriteLine("您输入的是春天的月份!");
    // }
    // else if (month > 5 && month < 9)
    // {
    // Console.WriteLine("您输入的是夏天的月份!");
    // }
    // else if (month > 8 && month < 12)
    // {
    // Console.WriteLine("您输入的是秋天的月份!");
    // }
    // else
    // {
    // Console.WriteLine("您输入的是冬天的月份!");
    // }
    //}
    //else
    //{
    // Console.WriteLine("输入有误!");
    //}

    练习

    //输入学生姓名,输入考试成绩 double
    //若是100,【恭喜你***,满分通过!】
    //若是大于等于80小于100,【**,你很优秀,继续保持!】
    //若是大于等于60小于80,【**成绩良好】
    //大于等于50小于60,【**就差一点点,下次一定要至少及格!】
    //小于50,【**你是笨蛋么?】
    //Console.Write("请输入您的姓名:");
    //string name = Console.ReadLine();
    //Console.Write("请输入您的分数:");
    //double score = double.Parse(Console.ReadLine());
    //if (score >= 0 && score <= 100)
    //{
    // if (score == 100)
    // {
    // Console.WriteLine(name+",恭喜你,满分通过!");
    // }
    // else if (score >= 80)
    // {
    // Console.WriteLine(name+",你很优秀!继续保持!");
    // }
    // else if (score >= 60)
    // {
    // Console.WriteLine(name+",成绩良好!");
    // }
    // else if (score >= 50)
    // {
    // Console.WriteLine(name + ",就差一点点!");
    // }
    // else
    // {
    // Console.WriteLine(name+",你是笨蛋么?");
    // }
    //}
    //else
    //{
    // Console.WriteLine("输入有误!");
    //}

    //Console.ReadLine();


    //请输入一个年份,判断此年份是不是闰年
    //year%4==0&&year%100!=0
    //year %400==0
    //Console.Write("请输入一个年份:");
    //int year = int.Parse(Console.ReadLine());
    //if (year >= 0 && year <= 9999)
    //{
    // if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    // {
    // Console.WriteLine("您输入的年份是闰年!");
    // }
    // else
    // {
    // Console.WriteLine("您输入的年份不是闰年!");
    // }
    //}
    //else
    //{
    // Console.WriteLine("输入有误!!");
    //}

    //分别输入年、月、日,判断日期格式是否正确!
    //Console.Write("请输入年份:");
    //int year = int.Parse(Console.ReadLine());
    //if (year >= 0 && year <= 9999)
    //{
    // Console.Write("请输入月份:");
    // int month = int.Parse(Console.ReadLine());
    // if (month >= 1 && month <= 12)
    // {
    // Console.Write("请输入日:");
    // int day = int.Parse(Console.ReadLine());
    // if (day >= 1 && day <= 31)
    // {
    // if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    // {
    // Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    // }
    // else if (month == 4 || month == 6 || month == 9 || month == 11)
    // {
    // if (day <= 30)
    // {
    // Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    // }
    // else//day==31
    // {
    // Console.WriteLine("日期格式错误!");
    // }
    // }
    // else//2月
    // {
    // if (day <= 28)
    // {
    // Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    // }
    // else if (day == 29)
    // {
    // if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    // {
    // Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    // }
    // else
    // {
    // Console.WriteLine("日期格式有误!!");
    // }
    // }
    // else//day==30||31
    // {
    // Console.WriteLine("日期格式有误!");
    // }
    // }
    // }
    // else
    // {
    // Console.WriteLine("日输入有误!!");
    // }
    // }
    // else
    // {
    // Console.WriteLine("月份输入有误!");
    // }
    //}
    //else
    //{
    // Console.WriteLine("年份输入有误!!");
    //}

    //Console.ReadLine();


    //1.方程ax^2+bx+c=0;一元二次方程。求根
    //△=b^2-4ac;若△<0方程无实根
    //若△>0,方程有两个不相同的实根x1 x2
    //若△=0,方程有两个相同的实根
    //Console.Write("请输入a=");
    //double a = double.Parse(Console.ReadLine());
    //if (a != 0)
    //{
    // Console.Write("请输入b=");
    // double b = double.Parse(Console.ReadLine());
    // Console.Write("请输入c=");
    // double c = double.Parse(Console.ReadLine());
    // double de = b * b - 4 * a * c;
    // if (de > 0)
    // {
    // double x1 = (-b + Math.Sqrt(de)) / (2 * a);
    // double x2 = (-b - Math.Sqrt(de)) / (2 * a);
    // Console.WriteLine("此方程有两个不同的实数根,x1={0},x2={1}",x1,x2);
    // }
    // else if (de == 0)
    // {
    // double x1 = (-b + Math.Sqrt(de)) / (2 * a);
    // Console.WriteLine("此方程有两个相同的实数根,x1=x2={0}", x1);
    // }
    // else//de<0
    // {
    // Console.WriteLine("此方程没有实数根!");
    // }
    //}
    //else//a==0
    //{
    // Console.WriteLine("此方程不是一元二次方程!");
    //}
    //Console.ReadLine();

    //2.相亲过程:你有房子么?你有钱么?你有能力么?
    //【结婚吧】【先买房子再结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】
    //利用if嵌套做相亲过程
    //Console.Write("你有房子么?");
    //string s = Console.ReadLine();
    //if (s == "有")
    //{
    // Console.WriteLine("结婚吧!");
    //}
    //else
    //{
    // Console.Write("那你有钱么?");
    // s = Console.ReadLine();
    // if (s == "有")
    // {
    // Console.WriteLine("先买房子再结婚!");
    // }
    // else
    // {
    // Console.Write("你有能力么?");
    // s = Console.ReadLine();
    // if (s == "有")
    // {
    // Console.WriteLine("先赚钱,再买房子,再结婚!");
    // }
    // else
    // {
    // Console.WriteLine("拜拜!!");
    // }
    // }
    //}

    //Console.ReadLine();

    //3.输入整数a和b,若a^2+b^2大于100,
    //则输出a^2+b^2百位以上数字,否则输出两数之和
    //Console.Write("请输入整数a");
    //double a = double.Parse(Console.ReadLine());
    //Console.Write("请输入整数b");
    //double b = double.Parse(Console.ReadLine());
    //if (a * a + b * b > 100)
    //{
    // Console.WriteLine(a*a+b*b);
    //}
    //else
    //{
    // Console.WriteLine(a+b);
    //}
    //Console.ReadLine();

    //4.有一组函数:
    //y = x (x<1);
    //y = 2x -1 (1<=x<10);
    //y = 3x-11 (x>=10)。
    //括号内是x的满足条件。
    //实现功能,随意输入一个x值,输出y的值。
    //Console.Write("请输入一个数值x:");
    //Double x = double.Parse(Console.ReadLine());

    //if(x<1)
    //{
    // Double y = x;
    // Console.WriteLine(y);
    //}
    //else if (x >= 1 && x < 10)
    //{
    // double y = 2 * x - 1;
    // Console.WriteLine(y);

    //}
    //else
    //{
    // double y = 3 * x - 11;
    // Console.WriteLine (y);
    //}

    //Console.ReadLine();

    2、//switch case 多选一
    Console.WriteLine("1.汉堡包");
    Console.WriteLine("2.鸡腿");
    Console.WriteLine("3.鸡米花");
    Console.WriteLine("4.鸡肉卷");
    Console.Write("请输入您最想吃的商品的编号:");
    int s = int.Parse(Console.ReadLine());
    switch (s)//括号内是变量名称
    {
    case 1:
    Console.WriteLine("您选择的是汉堡包!");
    break;//作用是跳出最近的花括号
    case 2:
    Console.WriteLine("您选择的是鸡腿!");
    break;//作用是跳出最近的花括号
    case 3:
    Console.WriteLine("您选择的是鸡米花!");
    break;//作用是跳出最近的花括号
    case 4:
    Console.WriteLine("您选择的是鸡肉卷!");
    break;//作用是跳出最近的花括号
    default://相当于else(表示以上可能都不是)
    Console.WriteLine("编号有误,没有此商品!");
    break;
    }
    Console.ReadLine();

    练习
    //分别输入月份 几号 输出是今年的第多少天
    //每年的1 3 5 7 8 10 12月是31天
    //今年的2月是28天
    //其他的4 6 9 11是30天

    //int m1 = 31; int m2 = 28; int m3 = 31; int m4 = 30; int m5 = 31; int m6 = 30; int m7= 31; int m8 = 31; int m9 = 30; int m10 = 31; int m11 = 30; int m12 = 31;
    //Console.WriteLine("请输入一个月份");
    //int m = int.Parse(Console.ReadLine() );
    //Console.WriteLine("请输入一个日号");
    //int d = int.Parse(Console.ReadLine());
    //switch (m)
    //{
    // case 1:
    // Console.WriteLine("第"+d.ToString()+"天");
    // break ;
    // case 2:
    // Console.WriteLine("第"+(m1+d).ToString()+"天");
    // break ;
    // case 3:
    // Console.WriteLine("第" + (m1 +m2+ d).ToString() + "天");
    // break;
    // case 4:
    // Console.WriteLine("第" + (m1 + m2 +m3+ d).ToString() + "天");
    // break;
    // case 5:
    // Console.WriteLine("第" + (m1 + m2 + m3+m4+d).ToString() + "天");
    // break;
    // case 6:
    // Console.WriteLine("第" + (m1 + m2 +m3+m4+m5+ d).ToString() + "天");
    // break;
    // case 7:
    // Console.WriteLine("第" + (m1 + m2 +m3+m4+m5+m6+ d).ToString() + "天");
    // break;
    // case 8:
    // Console.WriteLine("第" + (m1 + m2 +m3+m4+m5+m6+m7+ d).ToString() + "天");
    // break;
    // case 9:
    // Console.WriteLine("第" + (m1 + m2 + m3+m4+m5+m6+m7+m8+d).ToString() + "天");
    // break;
    // case 10:
    // Console.WriteLine("第" + (m1 + m2 +m3+m4+m5+m6+m7+m8+m9+ d).ToString() + "天");
    // break;
    // case 11:
    // Console.WriteLine("第" + (m1 + m2 + m3+m4+m5+m6+m7+m8+m9+m10+d).ToString() + "天");
    // break;
    // case 12:
    // Console.WriteLine("第" + (m1 + m2 + m3+m4+m5+m6+m7+m8+m9+m10+m11+d).ToString() + "天");
    // break;
    //}
    //Console.ReadLine();

  • 相关阅读:
    Day9
    Day9
    Day9
    洛谷 P1896 [SCOI2005]互不侵犯
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    Day9
    最大m段子段和 Day9
    jsp内置对象-application对象
    jsp内置对象-session对象
    jsp内置对象-response对象
  • 原文地址:https://www.cnblogs.com/yx1314520/p/5695631.html
Copyright © 2011-2022 走看看