C#小练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            我的第一个小程序
            string a = "我的";
            Console.Write("第一个程序\n");
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("这是{0}第一个程序:", a);
            Console.ReadKey();
            //计算2个数之和
            Console.WriteLine("请输入第一个数:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个数:");
            int b = Convert.ToInt32(Console.ReadLine());
            int sum = a + b;
            Console.WriteLine("两个数的和为:" + sum);
            Console.ReadKey();
            //随机
            double[] arry = new double[100];
            Random result = new Random();
            arry[0] = result.Next(200);
            Console.WriteLine(arry[0] / 100);
            for (int i = 0; i < arry[0] / 100; i++)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
            //随机数
            int[] arry = new int[100];
            Random result = new Random();
            Console.WriteLine(arry[0]);
            for (int i = 0; i < arry.Length; i++)
            {
                arry[i] = result.Next(200);
                Console.WriteLine(i);
            }
            Console.ReadKey();
            //输入数字判断星期
            Console.WriteLine("请输入:");
            int day = Convert.ToInt32(Console.ReadLine());
            int day = int.Parse(Console.ReadLine());
            switch (day)
            {
                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;
                case 8: Console.WriteLine("错误"); break;

            }
            Console.ReadKey();

            //简单随机数
            Random a = new Random();
            double b = a.Next(100, 200);
            Console.WriteLine(b);
            Console.ReadKey();
 
//    for语句求1-100之和
            int sum = 0;
            for (int i = 0; i <=100; i++)
            {
                //sum =sum + i;
                sum += i; 
            }
            Console.WriteLine(sum);
            Console.ReadKey();
            //do语句求1-100之和
            int sum = 0;
            int i = 1;
            do
            {
              sum += i;
                    i++; 
            } while (i <=100);
            Console.WriteLine(sum);
            Console.ReadKey();
            //while语句求1-100之和
            int i = 1, sum = 0;
            while (i<=100)
            {
                sum += i;
                i++;  
 
            }
            Console.WriteLine(sum);
            Console.ReadKey();
            //倒着输出
            string sd = "我是中国人";
            char[] arry = sd.ToCharArray();
            Array.Reverse(arry);
            sd = new string(arry);
            Console.WriteLine(sd);
            Console.ReadKey();
 

           
 
 
 
 
        }
    }
}
 
 
 方法一
static void Main(string[] args)
        {
            
            //求一个数的阶乘
            Console.WriteLine("请输入你要计算的值:");
            int j = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(fun(j).ToString()); 
            Console.ReadKey();
           
        }
            //定义一个方法
        public static double fun(int n)
        {
           
            double S = 1;
            for (int i = 0; i < n; i++)
            {
                S *= (i + 1);
                
            }
            return S;
        }
方法2
 Console.WriteLine("请输入你要计算的值:");
            int j = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(sum(j).ToString());
            Console.ReadKey();
           
        }
            public static double sum(int n) 
            {           
                if (n == 1)   
                    return 1;           
                else               
                 return sum(n - 1) * n;  
            }
 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值