• 函数及其调用


    class Program
        {
            public void dayin() 
            {
                Console.WriteLine("hollo world");
            }
            /// <summary>
            /// 累加求和
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public int leijia(int n) 
            {
                int sum = 0;
                for (int i = 1; i <= n;i++ )
                {
                    sum += i;
                }
                return sum;
            }
            /// <summary>
            /// 输入一个数判断是不是质数
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public void zhishu(int n) 
            {
                int count = 0;
                for (int i = 1; i <= n;i++ )
                {
                    if(n%i==0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    Console.WriteLine("质数");
                }
                else
                {
                    Console.WriteLine("不是质数");
                } 
            }
    
            /// <summary>
            /// 求阶乘及其的和
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public void jiecheng(int n) 
            {
                int jiecheng = 1;
                int he = 0;
                for (int i = 1; i <=n; i++)
                {
                    jiecheng *= i;
                    he = he + jiecheng;
                }
                Console.WriteLine("阶乘是"+jiecheng+"阶乘的和是"+he);
            }
            /// <summary>
            /// 猜拳三局两胜
            /// </summary>
            /// <param name="a"></param>
            /// <param name="b"></param>
            public void caiquan() 
            {
               
                int renying = 0;
                int diannaoying = 0;
                for (int i = 1; i <= 3; i++)
                {
                    Console.Write("请输入人出拳(0-剪刀,1-石头,2-布)a=");
                    int a = int.Parse(Console.ReadLine());
                    Random r = new Random();
                    int b = r.Next(3);
                    Console.WriteLine("电脑出拳(0-剪刀,1-石头,2-布)b=" + b);
                    
               
                    if (a - b == 1 || a - b == -2)
                    {
                        renying++;
                       Console.WriteLine("人赢一局");
                    }
                    else if (a == b)
                    {
                        Console.WriteLine("平局");
                        i--;
                        continue;
                    }
                    else
                    {
                        diannaoying++;
                        Console.WriteLine("电脑赢一局");
                    }
                    if (renying >= 2)
                    {
                        Console.WriteLine("三局两胜人赢");
                        break;
                    }
                    else if (diannaoying >= 2)
                    {
                        Console.WriteLine("三局两胜电脑赢");
                        break;
                    }
                }
            }
            //主函数
            static void Main(string[] args)
            {
                while (true)
                {
                    new Program().dayin();
                    Console.WriteLine("请输入一个正整数");
                    int n = int.Parse(Console.ReadLine());
    
                    int jieguo = new Program().leijia(n);
                    Console.WriteLine(jieguo);
                    new Program().zhishu(n);
                    new Program().jiecheng(n);
                    new Program().caiquan();
                    Console.ReadLine();
                }
            }
        }

  • 相关阅读:
    线性表之顺序表的结构与实现
    (转)面试题1:第一个只出现一次的字符
    c++ 虚基类应用
    c++ 多重继承
    c++ 单继承派生类的构造函数
    c++ 构造函数
    深入 Struts2 的配置
    c++ 结构体
    深入浅出C指针
    HTML5 小实例
  • 原文地址:https://www.cnblogs.com/wang-kaifeng/p/4828416.html
Copyright © 2020-2023  润新知