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;
}
C#小练习
最新推荐文章于 2023-01-16 13:03:52 发布