1.折叠代码
#region
折叠内容
#endregion
2.保留三位小数
using System; namespace _005_double保留3位小数 { class Program { static void Main(string[] args) { Console.WriteLine("输入三位数,依次按Enter结束:"); double a = double.Parse(Console.ReadLine()); double b = double.Parse(Console.ReadLine()); double c = double.Parse(Console.ReadLine()); Console.WriteLine("{0:f3}",a); Console.WriteLine("{0,10:f3}",b); Console.WriteLine("{0,10:f3}",c); } } }
3.求水仙花数
using System; namespace _001_水仙花数 { class Program { static void Main(string[] args) { int[] numbers = new int[3]; int sum = 0; for (int num = 100; num < 1000; num++) { numbers[0] = num / 100 ; numbers[1] = num %100/10; numbers[2] = num % 100%10; //for (int i = 0; i < 3; i++) //{ // sum += (int)Math.Pow(numbers[i], 3); //} sum = numbers[0] * numbers[0] * numbers[0] + numbers[1] * numbers[1] * numbers[1] + numbers[2] * numbers[2] * numbers[2]; if (num == sum) { Console.WriteLine("水仙花数为:" + num); } } } } }
4.
四舍五入并保留两位小数
double d =3.1415926;
d= Math.Round(d,2);
Console.WriteLine("{0,10:f2}",d)//另一种方法,冒号前位10的倍数,显示小数点前有多少字节,f后要保留的小数 位数
using System; namespace _008_四舍五入 { class Program { static void Main(string[] args) { Console.WriteLine("输入以为正数:"); double a = Double.Parse(Console.ReadLine()); int result = (int)(a + 0.5); //double b = Math.Ceiling(a);//向上进位取整 ////double b = Math.Floor(a);//向下进位取整 Console.WriteLine(result); } } }
5.
编一个程序,用while循环语句来计算1+1/2+2/3+3/4+...+99/100之和。
using System; namespace _013_while求和 { class Program { static void Main(string[] args) { double i = 1; double sum = 1; while (i<=100) { sum =sum+(i-1)/i ; i++; } Console.WriteLine("和为{0}",sum); } } }
6.
编写一个程序,用for语句,求出1+(1+2)+(1+2+3)+...+(1+2+3+...+10)之和。
using System; namespace _014_for求和 { class Program { static void Main(string[] args) { int sum = 0; int sum1 = 0; for (int i = 1; i <=10; i++) { sum = sum + i; sum1 = sum1 + sum; } Console.WriteLine(sum1); } } }
7.编一个程序,要求使用while语句,输入用户名和密码,实现用户登录程序的功能,至多允许输入三次,超过三次不允许登录。
using System; namespace _016_加分题2 { class Program { static void Main(string[] args) { Console.WriteLine("请输入用户名和密码:"); int count = 0; while (count <3) { string name = Console.ReadLine(); string password = Console.ReadLine(); if (name == "huang" && password == "154090") { Console.WriteLine("登录成功"); break; } else Console.WriteLine("错误,请重新输入!"); count++; } } } }
8.求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
using System; namespace _005_能被7整除的数 { class Program { static void Main(string[] args) { int count = 0; int sum = 0; for(int i = 1; i <= 1000; i++) { if ( i % 7 == 0) { sum += i; count++; Console.WriteLine(i); } if(count == 5) { Console.WriteLine(sum); sum = 0; count = 0; } } } } }
8.,编写一个控制台程序,分别输出1~100之间的平方、平方根。
using System; namespace _006_1_100之间的平方平方根 { class Program { static void Main(string[] args) { for (int i= 1; i <= 100; i++) { double a = Math.Pow(i, 0.5);//平方 double b = Math.Pow(i, 2); //double gen = Math.Sqrt(i);//这个方法可以取得一个数字的平方根 Console.WriteLine("{2}的平方为{0},平方根为{1}",b,a,i); } } } }
9.判断月份天数
using System; namespace _009_输出该月天数 { class Program { static void Main(string[] args) { //闰年:2月29 1,3,5,7,---31天 4,6,9,11---30 8,10,12----31 //平年:28 Console.WriteLine("请输入年份,月份,分别按Enter结束"); int year = int.Parse(Console.ReadLine()); int month = int.Parse(Console.ReadLine()); if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//闰年情况下 { if(month % 2 != 0&&month<8) { Console.WriteLine("31天"); } else if(month == 2) { Console.WriteLine("29天"); } else if(month % 2 == 0 && month < 8) { Console.WriteLine("30天"); } else if (month % 2 == 0 && month >= 8) { Console.WriteLine("31天"); } else if (month% 2 != 0 && month >= 8) { Console.WriteLine("30天"); } } else { if (month % 2 != 0 && month < 8) { Console.WriteLine("31天"); } else if (month == 2) { Console.WriteLine("28天"); } else if (month % 2 == 0 && month < 8) { Console.WriteLine("30天"); } else if (month % 2 == 0 && month >= 8) { Console.WriteLine("31天"); } else if (month % 2 != 0 && month >= 8) { Console.WriteLine("30天"); } } } } }
10.1-1000之间的素数
using System; namespace _020_1_1000之间的素数 { class Program { static void Main(string[] args) { for (int i = 2; i < 1001; i++) { bool sushu = true;//布尔类型 for (int j = 2; j <=i-1; j++) { if (i % j == 0) { sushu = false; break; } } if (sushu) { Console.WriteLine(i); } } } } }
11.九九乘法表
using System; namespace _021__九九乘法表 { class Program { static void Main(string[] args) { //第一种格式 //for (int i = 1; i < 10; i++) //{ // for (int j = 1; j < 10; j++) // { // Console.Write(i+"*"+j+"="+(i*j)+" ");//不换行 // } // Console.WriteLine();//什么都没有表示换行 //} //第二种格式 for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { Console.Write(i + "*" + j + "=" + (i * j) + " ");//不换行 } Console.WriteLine();//什么都没有表示换行 } } } }
12.
生成随机数
Random random = new Random();//创建一个可以生成随机数的对象
int num = random.Next(1,7);//给一个范围,取不到最大值
Console.WriteLine(num);
using System; namespace _022_掷骰子_打印各种点数的出现次数 { class Program { static void Main(string[] args) { int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0; Random random = new Random();//创建随机数 for (int i = 0; i < 100; i++) { int num = random.Next(1, 7);//范围1到6 switch (num) { case 1: num1++; break; case 2: num2++; break; case 3: num3++; break; case 4: num4++; break; case 5: num5++; break; case 6: num6++; break; } } Console.WriteLine(num1); Console.WriteLine(num2); Console.WriteLine(num3); Console.WriteLine(num4); Console.WriteLine(num5); Console.WriteLine(num6); } } }
13.金字塔
using System; namespace _004_金字塔 { class Program { static void Main(string[] args) { for (int i = 0; i < 9; i++) { for (int j = i; j < 8; j++) { Console.Write(" "); } for (int z = 0; z <= 2*i; z = z + 1) { Console.Write("*"); } Console.WriteLine(); } } } }
14.四舍五入
using System; namespace _006_四舍五入 { class Program { static void Main(string[] args) { Console.WriteLine("输入一位正数:"); double a = Double.Parse(Console.ReadLine()); //int result = (int)(a + 0.5);这方法也可以 a = Math.Round(a, 0); //double b = Math.Ceiling(a);//向上进位取整 ////double b = Math.Floor(a);//向下进位取整 Console.WriteLine(a); } } }
15.枚举之星期
using System; namespace _010_QQ状态 { public enum QQ { OnLine=1, OffLine=2, Leave=3, Qme=4, Busy=5 } class Program { static void Main(string[] args) { Console.WriteLine("输入数字"); int index = int.Parse(Console.ReadLine()); QQ num = (QQ)index; switch (num) { case QQ.OnLine: case QQ.OffLine: case QQ.Leave: case QQ.Qme: case QQ.Busy: Console.WriteLine(num); break; default: Console.WriteLine("超出范围"); break; } } } }
16。键盘输入红包金额,包数
1. using System; namespace _001_随机红包System.Security.Cryptography.RNGCryptoServiceProvider { class Program { static void Main(string[] args) { //微信红包 Console.WriteLine("请输入红包金额和数量:"); int money = int.Parse(Console.ReadLine()); int num = int.Parse(Console.ReadLine()); int dollar = money * 100; int temp = 0; Random a = new Random(); for (int i = 0; i < num - 1; i++) { int b = a.Next(1, dollar - temp); if (b > dollar / 2) { i--; continue; } temp += b; if (temp == dollar) { temp = 0; i = 0; continue; } Console.WriteLine(b / 100.0); } Console.WriteLine((dollar - temp) / 100.0); } } } 2. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("请输入金额(例如:100.00)"); double money = Math.Round(Convert.ToDouble(Console.ReadLine()), 2); Console.WriteLine("请输入红包个数"); int count = Convert.ToInt32(Console.ReadLine()); List<double> listMoney = new List<double>(); for (int i = 0; i < count; i++) { Random random = new Random(); if (i == 0) { listMoney.Add(money); continue; } double maxMoney = listMoney.Max(); int maxIndex = listMoney.IndexOf(maxMoney); double rd = random.NextDouble(); double m = Math.Round(maxMoney * rd, 2); listMoney[maxIndex] = maxMoney - m; listMoney.Add(m); } Console.WriteLine("分别获得红包"); foreach (var item in listMoney) { Console.WriteLine("{0} 元", item.ToString("N")); } //测试,计算金额是否正确 Console.WriteLine("合计:{0}元 ", listMoney.Sum().ToString("N")); Console.WriteLine("按任意键继续 "); Console.ReadKey(); } } } }