找到 34423 篇文章 相关编程
166 次查看
要查找平均值,首先设置一个数组 int[] myArr = new int[10] { 45, 23, 55, 15, 8, 4, 2, 5, 9, 14 }; 现在找到总和并将其除以数组的长度以获得平均值。 int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; average = sum / len; } 让我们看看完整的代码 - 示例 using System; public class Program { public static void Main() { int[] myArr = new int[10] { 45, 23, 55, 15, 8, 4, 2, 5, 9, 14 }; int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len; Console.WriteLine("Sum = " + sum); Console.WriteLine("Average Of integer elements = " + average); } }
168 次查看
以下是我们的整数数组 - int[] myArr = new int[6] { 8, 4, 2, 5, 9, 14 }; 首先,获取数组的长度,并循环遍历数组以找到元素的总和。之后,将其除以长度。 int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len; 以下是完整的代码示例 实时演示 using System; public class Program { public static void Main() { ... 阅读更多
1K+ 次查看
使用 Replace() 方法用星号替换字符。 假设我们的字符串是 - string str = "dem* text"; 要替换它,请使用 Replace() 方法 - str.Replace('*', 'o'); 以下是完整的代码 - 示例 实时演示 using System; public class Program { public static void Main() { string str = "dem* text"; Console.WriteLine("Initial string = " + str); string res = str.Replace('*', 'o'); // 替换后 Console.WriteLine("After replacing asterisk = " + res.ToString()); } } 输出 Initial string = dem* text After replacing asterisk = demo text
5K+ 次查看
假设我们的字符串是 - string str = "abcd$ef$gh"; 要替换特殊字符,请使用 Replace() 方法。 string res = str.Replace('$', 'k'); 以下是替换字符串中字符的完整代码 - 示例 实时演示 using System; public class Program { public static void Main() { string str = "abcd$ef$gh"; Console.WriteLine("Initial string = " + str); string res = str.Replace('$', 'k'); // 替换后 Console.WriteLine("Replaced string = " + res.ToString()); } } 输出 Initial string = abcd$ef$gh Replaced string = abcdkefkgh
1K+ 次查看
设置一个包含重复单词的字符串。 string str = "One Two Three One"; 上面,您可以看到单词“One”出现了两次。 要删除重复的单词,您可以尝试在 C# 中运行以下代码 - 示例 实时演示 using System; using System.Linq; public class Program { public static void Main() { string str = "One Two Three One"; string[] arr = str.Split(' '); Console.WriteLine(str); var a = from k in arr orderby k select k; Console.WriteLine("After removing duplicate words..."); ... 阅读更多
194 次查看
首先,声明一个 HashSet 并添加元素 - var names = new HashSet(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin"); 要删除元素,请使用 RemoveWhere。 names.RemoveWhere(x => x == "John"); 让我们看看完整的示例 - 示例 using System; using System.Collections.Generic; public class Program { public static void Main() { var names = new HashSet < string > (); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin"); Console.WriteLine("Initial Set..."); foreach(var val in names) { Console.WriteLine(val); } names.RemoveWhere(x => x ... 阅读更多
6K+ 次查看
设置列表。 List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32); 要获取唯一元素。 List myList = list.Distinct().ToList(); 以下是显示列表中唯一值的完整示例。 示例 实时演示 using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(55); list.Add(45); list.Add(55); list.Add(65); list.Add(73); list.Add(24); list.Add(65); Console.WriteLine("Initial List..."); ... 阅读更多
538 次查看
声明一个列表并添加元素。 List list = new List(); list.Add(50); list.Add(90); list.Add(50); list.Add(100); 现在,使用 Distinct() 方法仅获取唯一元素。 List myList = list.Distinct().ToList(); 以下是从列表中删除重复元素的完整代码 - 示例 实时演示 using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(50); list.Add(90); list.Add(50); list.Add(100); Console.WriteLine("Initial List..."); foreach(int a in list) ... 阅读更多
752 次查看
设置第一个列表。int[] arr1 = { 65, 57, 63, 98 };现在,设置第二个列表。int[] arr2 = { 43, 65, 33, 57 };现在让我们看看使用 == 和 < 运算符检查两个列表是否具有共同元素的完整代码。示例 在线演示使用 System;使用 System.Collections.Generic;使用 System.Linq;public class Program { public static void Main() { int[] arr1 = { 65, 57, 63, 98 }; ... 阅读更多
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP