zoukankan      html  css  js  c++  java
  • C#第五章学习代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._1_分支语句之if语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                //判断变量a中存放数值与10的关系
                Console.WriteLine("请输入数值,判断它与10的关系:");
                int a = int.Parse(Console.ReadLine());
                //int.Parse用于将屏幕输入的语句转换为整型
                if (a<10)
                {
                    Console.WriteLine("您输入的数什小于10");
                }
               else if (a==10)
                {
                    Console.WriteLine("您输入的数值等于10");
                }
               else//如果前边的if语句一条也没有执行,那就执行else语句
               //如果执行了其中一条if句语,那就不会执行else语句
                {
                    Console.WriteLine("您输入的数值大于10");
                }
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._2_分支语句之switch语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                //输入1显示星期一,依次类推
                Console.WriteLine("请输入月分数");
                int month = int.Parse(Console.ReadLine());
                //if (week == 1) Console.WriteLine("星期一");
                //if (week == 2) Console.WriteLine("星期二");
                //if (week == 3) Console.WriteLine("星期三");
                //if (week == 4) Console.WriteLine("星期四");
                //if (week == 5) Console.WriteLine("星期五");
                //if (week == 6) Console.WriteLine("星期六");
                //if (week == 7) Console.WriteLine("星期日");
    
                //switch (week)
                //{
                //    case 1: Console.WriteLine("星期一"); break;
                //    case 2: Console.WriteLine("星期二"); break;
                //    case 3: Console.WriteLine("星期三"); break;
                //    case 4: Console.WriteLine("星期四"); break;
                //    case 5: Console.WriteLine("星期五"); break;
                //    case 6: Console.WriteLine("星期六"); break;
                //    case 7: Console.WriteLine("星期日"); break;
                //    default: Console.WriteLine("您的输入有误"); break;
    
    
    
                //}
    
                //判断2015年每个月份的天数
                //31天:1、3、5、7、8、10、12
                //30天:4、6、9、11
                //28天:2
    
                switch(month)
    {
                    case 2: Console.WriteLine("本月有28天");break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        Console.WriteLine("本月有30天");break;
                    default: Console.WriteLine("本月有31天");break;
    
                }
    
    
                Console.ReadLine();
    
            }
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._3分支语句之三元运算符
    {
        class Program
        {
            static void Main(string[] args)
            {
                //提示用户进行输入
                Console.WriteLine("请输入您需要比较的数值");
                int num = int.Parse(Console.ReadLine());
                Console.WriteLine(num < 10 ? "您输入的数值小于10" : "您输入的数值大于等于10");
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._4_迭代语句之while语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                //输出1-50到屏幕上
                //Console.WriteLine(1);
                //Console.WriteLine(2);
                //Console.WriteLine(3);
                //利用while可以大量简化重复执行的代码
    
                int num = 1;
                while (num <= 50)
                {
                    Console.WriteLine(num);
                    num++;
    
                }
                Console.ReadKey();
            }
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._5_迭代语句之do._.while语句
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //输出1-50到屏幕上
                //int num = 0;
                //do
                //{
                //    num++;
                //    Console.WriteLine(num);
                //} while (num !=0);
                double Balance=0;
                double Rate = 0;
                int Year = 0;
                double TargetBalance = 0;
                Console.WriteLine("请输入您的本金");
                Balance = double.Parse(Console.ReadLine());
                Console.WriteLine("请输入当前的利率百分比");
                Rate = double.Parse(Console.ReadLine())/100;
                Console.WriteLine("请输入您的目标收益");
                //验证输入的有效性
                do
                {
                    TargetBalance = double.Parse(Console.ReadLine());
                    if (TargetBalance <= Balance) Console.WriteLine("您没有必要存钱,请重新输入一个比本金大的收益");
    
                } while (TargetBalance<=Balance);
                do
                {
                    Balance *= (Rate+1);
                    Year++;
    
                } while (Balance<TargetBalance);
                Console.WriteLine("您将在{0}年内,获得{1}元的收益",Year,Balance);
    
                Console.ReadLine();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._6_迭代语句之for循环语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                //求输入数的阶乘
                //1!=1 2!=2×1 3!=3×2×1
                //Console.WriteLine("请输入需要计算阶乘的数");
                //for(;;)
                //{ 
                //int a =int.Parse(Console.ReadLine());
                //int jc = 1;
                //for (int i=1;i<=a;i++)
                //{
                //    jc *= i;
                //}
                //Console.WriteLine("阶乘的结果是"+jc);
                Console.WriteLine("==============九九乘法表============");
                for (int i = 1; i < 10; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        Console.Write("{0}×{1}={2} ", j, i, j * i);
                        if (i * j < 10) Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
             
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._7_迭代语句之for循环语句_2_
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("=====================九九乘法表=====================");
                //使用for循环时,一般在for循环语句进行声明循环计次的变量
                for (int i = 1; i < 10; i++)
                {
    
                    for (int j = 1; j <= i; j++)
                    {
                        Console.Write("{0}×{1}={2} ",j,i,j*i);
                        if (i * j < 10) Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._8_迭代语句之foreach语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                //for (int i = 1; i <= 50; i++)
                //{
    
                //    Console.WriteLine(i);
                //}
    
                //for (int i = 0; i < =50; i++)
                //{
                //    Console.WriteLine(i);
                //}
                //将语句识别为单词并逐行输出
                //语句用string类型  字母用char
                Console.WriteLine("请您输入一个英文句子");
                string str = Console.ReadLine();
                Console.WriteLine("您输入的句子包含以下的单词");
                foreach (char word in str)
                {
                    if (char.IsWhiteSpace(word))
                    {
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write(word);
                        //word = 't';//foreach语句的迭代变量不允许重新赋值
                    }
                }
    
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._9_跳转语句之break语句
    {
        class Program
        {
            static void Main(string[] args)
            {
    
    
                Console.WriteLine("输出1-500的数字,每行10个");
                for (int i = 1; i < 501; i++)
                {
                    if ((i % 2 == 0) && (i % 3 == 0) && (i % 4 == 0) && (i % 5 == 0) && (i % 6 == 0) && (i % 7 == 0))
                    {
                        Console.Write(i);
                        Console.WriteLine();
                        Console.WriteLine(i+"就是我们想要的那个数");
                        break;
                    }
    
                    if (i % 10 == 0) Console.WriteLine(i);
                    else
                    {
    
                        Console.Write(i+" ");
                        if (i < 10) Console.Write(" ");
                        if (i < 100) Console.Write(" ");
                    }
    
                }
                Console.ReadLine();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._10_跳转语句之continue语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("50以内的奇数有:");
                for (int i = 1; i < 51; i++)
                {
                    if (i % 2 == 0) continue;
                    //continue语句用于停止本次迭代,并非跳出迭代语句
                    Console.Write(i+"	");
                }
                Console.ReadLine();
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._11_跳转语句之return语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                while(true)
                {
    
                
                Console.WriteLine("请输入三个整数,按回车键确认每个数的输入:");
                int a = int.Parse(Console.ReadLine());
                int b = int.Parse(Console.ReadLine());
                int c = int.Parse(Console.ReadLine());
                    average(a,b,c);
                    //double average = (a+b+c)/3;
                    //double Average = average(a,b,c);
                //Console.ReadLine();
    }
            }
    
            static void  average(int a, int b, int c)
            {
                Console.WriteLine("您输入三个整数的平均值是:{0}", (a + b + c) / 3);
    
                return ;
            }      
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._12_跳转语句之goto语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 0;
                Console.WriteLine("5的阶乘等于几?根据以下选项选择正确答案,回车键确认:");
                Console.WriteLine("1. 5!=5
    2. 5!=10
    3. 5!=20
    4. 5!=60");
                error:
                {
                    a++;
                    if (a > 1) Console.WriteLine("很遗憾,您答错了,请重新输入答案.");
    
                }
                int option = int.Parse(Console.ReadLine());
                switch (option)
                {
                    case 1:
                    case 2:
                    case 3: goto error;
                    case 4:goto right;
                    default:
                        Console.WriteLine("您选择的选项不存在");
                        goto end;
    
                }
    
                right:
    
                Console.WriteLine("恭喜您,答对了!");
                end:
                Console.ReadKey();
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5._13_本章小结及任务实施
    {
        class Program
        {
            static void Main(string[] args)
            {
                while(true)
                {
                Console.WriteLine("请输入三个整数,我们将给您算出中间数和它的阶乘");
                int a = int.Parse(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());
                int c = int.Parse(Console.ReadLine());
                int middleNum = 0;
                //a是中间值,那么有两种情况,b是最小值或都b是最大值
                //b
                //c
                if (a >= b && a <= c || a >= c && a <= b)
                {
                    Console.WriteLine(a + "是中间值");
                    middleNum = a;
    
                }
                if (b >= a && b <= c || b >= c && b <= a)
                {
                    Console.WriteLine(b + "是中间值");
                    middleNum = b;
                }
                if (c >= a && c <= b || c >= b && c <= a)
                {
                    Console.WriteLine(c + "是中间值");
                    middleNum = c;
                }
    
                int result = 1;
                for (int i = 1; i <= middleNum; i++)
                {
                    result *= i;
                }
                Console.WriteLine("它的阶乘是" + result);
            }
    
                Console.ReadKey();
    
    
    
            }
        }
    }
  • 相关阅读:
    State Management
    排序效率小PK
    ES,ZK,Mysql相关参数优化
    ES翻译之Function Score Query
    打算坚持学习技术的习惯
    面试——并发基础知识1
    码云的GIT操作
    CountDownLatch模拟高并发测试代码
    Bloom Filter的应用
    面试题1—选取同一个字符第一不重复的字符
  • 原文地址:https://www.cnblogs.com/zqyo2000z/p/5338787.html
Copyright © 2011-2022 走看看