找到 34423 篇文章 关于编程
193 次浏览
要查找数字的幂,首先设置数字和幂:int n = 15; int p = 2; 现在创建一个方法并传入这些值:static long power(int n, int p) { if (p != 0) { return (n * power(n, p - 1)); } return 1; } 上面的递归调用给了我们结果:n * power(n, p - 1) 下面是获取数字幂的完整代码:示例 在线演示using System; using System.IO; public class Demo { public static void Main(string[] args) { ... 阅读更多
600 次浏览
创建新线程:Thread thread = Thread.CurrentThread; thread.Name = "My new Thread"; 要获取当前上下文 ID,请使用 ContextID 属性:Thread.CurrentContext.ContextID 让我们看看完整的代码:示例 在线演示using System; using System.Threading; namespace Demo { class MyClass { static void Main(string[] args) { Thread thread = Thread.CurrentThread; thread.Name = "My new Thread"; Console.WriteLine("线程名称 = {0}", thread.Name); Console.WriteLine("当前上下文 ID:{0}", Thread.CurrentContext.ContextID); Console.ReadKey(); } } }输出线程名称 = My new Thread 当前上下文 ID:0
1K+ 次浏览
创建一个使用递归的方法来获取第 n 个值:public int displayFibonacci(int n) 调用该方法:displayFibonacci(val) 调用时,displayFibonacci() 方法被调用并使用递归计算第 n 个值:public int displayFibonacci(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return displayFibonacci(n - 1) + displayFibonacci(n - 2); } } 让我们看看完整的代码:示例 在线演示using System; public class Demo { public static void Main(string[] args) { Demo d = ... 阅读更多
2K+ 次浏览
要查找两个二进制数的和,首先设置它们:val1 = 11110; val2 = 11100; 现在调用 displaySum() 方法,该方法用于显示和:sum = displaySum(val1, val2); 我们在方法中设置了一个新数组来显示二进制数的每一位:long[] sum = new long[30]; 现在让我们看看计算二进制数和的完整代码,如下所示:示例 在线演示using System; class Demo { public static void Main(string[] args) { long val1, val2, sum = 0; val1 = 11110; val2 ... 阅读更多
674 次浏览
要查找字符串中每个单词的频率,您可以尝试运行以下代码:示例 在线演示using System; class Demo { static int maxCHARS = 256; static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; } public static void Main() { String s = "Football!"; int []cal = new int[maxCHARS]; calculate(s, cal); for (int i = 0; i < maxCHARS; i++) if(cal[i] > ... 阅读更多
2K+ 次浏览
要获取变量的大小,使用 sizeof:int x; x = sizeof(int); 要获取变量的大小,不使用 sizeof,请尝试以下代码:// 不使用 sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length; 这是完整的代码。示例 在线演示using System; class Demo { public static void Main() { int x; // 使用 sizeof x = sizeof(int); Console.WriteLine(x); // 不使用 sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length; Console.WriteLine(d); } }输出4 4
10K+ 次浏览
在 C# 中,使用 readonly 来声明常量数组:public static readonly string[] a = { "Car", "Motorbike", "Cab" }; 与 const 不同,在 readonly 中,您也可以在运行时设置值。实现上述目标的另一种方法:public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", "Cab" }.AsReadOnly();}} .NET Framework 4.5 对我们看到的内容进行了改进:public ReadOnlyCollection a { get; } = new ReadOnlyCollection( new string[] { "Car", "Motorbike", "Cab" } );
215 次浏览
要查找数字是否可被 2 整除,请检查数字可被 2 整除时会发生什么。如果余数为 0,则该数字可被 2 整除,否则为假:if (num % 2 == 0) { Console.WriteLine("可被 2 整除"); } else { Console.WriteLine("不可被 2 整除"); } 下面是完整的代码:示例 在线演示using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Test { static void Main(string[] args) { int num; num = 18; ... 阅读更多
884 次浏览
假设我们的字符串是:String s = "HeathLedger!"; 现在创建一个新数组:int []cal = new int[maxCHARS]; 创建一个新方法并将字符串和新数组都传入其中。查找字符的最大出现次数:static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; } 让我们看看完整的代码:示例 在线演示using System; class Demo { static int maxCHARS = 256; static void calculate(String s, int[] cal) { for (int i = 0; i < s.Length; i++) cal[s[i]]++; } ... 阅读更多
3K+ 次浏览
声明并初始化一个列表。var products = new List (); products.Add("Accessories"); products.Add("Clothing"); products.Add("Footwear"); 要获取大小,请使用 Capacity 属性。products.Capacity 现在让我们看看查找列表大小的完整代码。示例 在线演示using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { var products = new List
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP