把经常用的程序写成函数,直接调用,可以减少编写程序的工作量。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { public static bool zhishu(int shu) { int c = 0; for (int i = 1; i <= shu; i++) { if (shu % i == 0) { c++; } } if (c == 2) { return true; } else { return false; } } public void bzz(string s, int t, int g) { if (s == "男") { double n = t - g + 100; if (n >= -3 && n <= 3) { Console.WriteLine("标准"); } else if (n > 3) { Console.WriteLine("偏胖"); } else { Console.WriteLine("偏瘦"); } } else if (s == "女") { double n = t - g + 110; if (n >= -3 && n <= 3) { Console.WriteLine("标准"); } else if (n > 3) { Console.WriteLine("偏胖"); } else { Console.WriteLine("偏瘦"); } } else { Console.WriteLine("输入有误"); } } static void Main(string[] args) { Console.WriteLine("请输入一个数"); int b = int.Parse(Console.ReadLine()); bool a = Program.zhishu(b); if (a) { Console.WriteLine("是质数"); } else { Console.WriteLine("不是质数"); } Console.WriteLine("请输入性别"); string c = Console.ReadLine(); Console.WriteLine("请输入体重"); int d = int.Parse(Console.ReadLine()); Console.WriteLine("请输入身高"); int e = int.Parse(Console.ReadLine()); Program p = new Program(); p.bzz(c, d, e); Console.ReadLine(); } } }
质数 身高体重标准
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication7 { class Program { public static int[] paixu (int []arr)//定义数组,冒泡排序 { for (int i = 0; i <= arr.Length; i++) { for (int j = i; j <= arr.Length - 1; j++) { if (arr[i] < arr[j]) { int zhong = 0; zhong = arr[i]; arr[i] = arr[j]; arr[j] = zhong; } } } for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i]+" "); } return arr;//排好之后输出 } static void Main(string[] args) { Console.WriteLine("请输入人数");//输入人数 int renshu = int.Parse(Console.ReadLine()); int[] arr = new int[renshu]; for (int h = 1; h <= renshu; h++) { Console.WriteLine("请输入第{0}个人的成绩", h);//输入成绩 arr[h - 1] = int.Parse(Console.ReadLine()); } Program.paixu(arr);//调用函数,输出 Console.ReadLine(); } } }
冒泡排序