找到 34423 篇文章,主题为编程

C#程序:查找列表中最大、最小、第二大、第二小的数字

Samual Sam
更新于 2020年6月22日 09:31:34

2K+ 阅读量

设置列表var val = new int[] { 99, 35, 26, 87 };现在获取最大值:val.Max(z => z);最小值:val.Min(z => z);第二大值:val.OrderByDescending(z => z).Skip(1).First();第二小值:val.OrderBy(z => z).Skip(1).First();以下为代码示例:示例 在线演示using System; using System.Linq; public class Program { public static void Main() { var val = new int[] { 99, 35, 26, 87 }; var maxNum = val.Max(z => z); ... 阅读更多

C#程序:查找整数数组中所有重复元素

karthikeya Boyini
更新于 2020年6月22日 09:35:59

8K+ 阅读量

首先,设置包含重复元素的数组:int[] arr = { 24, 10, 56, 32, 10, 43, 88, 32 };现在声明一个字典,并循环遍历数组以获取重复元素:var d = new Dictionary(); foreach(var res in arr) { if (d.ContainsKey(res)) d[res]++; else d[res] = 1; }示例 在线演示using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { ... 阅读更多

C#程序:将时间从12小时制转换为24小时制

George John
更新于 2020年6月22日 09:36:23

11K+ 阅读量

首先,设置12小时制日期:DateTime d = DateTime.Parse("05:00 PM");现在将其转换为24小时制:d.ToString("HH:mm"));以下是将时间从12小时制转换为24小时制的代码:示例 在线演示using System; namespace Demo { public class Program { public static void Main(string[] args) { DateTime d = DateTime.Parse("05:00 PM"); Console.WriteLine(d.ToString("HH:mm")); } } }输出17:00

C#程序:将偶数和奇数整数分别拆分为不同的数组

Samual Sam
更新于 2020年6月22日 09:37:39

1K+ 阅读量

取两个数组:int[] arr2 = new int[5]; int[] arr3 = new int[5];现在,如果数组元素除以2余数为0,则为偶数。获取这些元素并添加到另一个数组中。这将循环遍历数组的长度:if (arr1[i] % 2 == 0) { arr2[j] = arr1[i]; }在else条件中,您将获得奇数元素。将它们添加到单独的数组中,并如以下示例所示分别显示它们:示例 在线演示using System; namespace Demo { public class Program { public static void Main(string[] args) { ... 阅读更多

C#程序:执行货币转换

Chandu yadav
更新于 2020年6月22日 09:19:32

4K+ 阅读量

假设您需要获取10美元的印度卢比价值。首先,设置变量:double usd, inr, val;现在设置美元并将其转换为印度卢比。// 美元数量 usd = 10; // 美元的当前价值 val = 69; inr = usd * val;让我们看看完整的代码:示例 在线演示using System; namespace Demo { public class Program { public static void Main(string[] args) { Double usd, inr, val; // 美元数量 usd = 10; // 美元的当前价值 val = 69; inr = usd * val; Console.WriteLine("{0} Dollar = {1} INR", usd, inr); } } }输出10 Dollar = 690 INR

C#程序:打印给定整数数组中所有不同的元素

karthikeya Boyini
更新于 2020年6月22日 09:22:53

449 阅读量

我们设置了一个数组和一个字典来获取不同的元素。int[] arr = { 88, 23, 56, 96, 43 }; var d = new Dictionary();字典集合允许我们获取列表的键和值。以下是显示给定整数数组的不同元素的代码:示例 在线演示using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 88, ... 阅读更多

C#程序:确定数组中是否存在两个整数的和等于给定整数

Arjun Thakur
更新于 2020年6月22日 09:21:51

850 阅读量

以下是我们的数组:int[] arr = new int[] { 7, 4, 6, 2 };假设给定的整数应该等于另外两个整数的和:int res = 8;获取和并查找相等性:for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length; j++) { if (i != j) { int sum = arr[i] + arr[j]; if (sum == res) { Console.WriteLine(arr[i]); ... 阅读更多

如何在C#中将列表转换为字符串?

Ankith Reddy
更新于 2020年6月22日 09:25:18

2K+ 阅读量

声明一个列表:List l = new List();现在,向列表中添加元素:// 元素 l.Add("Accessories"); l.Add("Footwear"); l.Add("Watches");现在将其转换为字符串:string str = string.Join(" ", l.ToArray());让我们看看在C#中将列表转换为字符串的最终代码:示例using System; using System.Collections.Generic; class Demo { static void Main() { List l = new List(); // 元素 l.Add("Accessories"); l.Add("Footwear"); l.Add("Watches"); string str = string.Join(" ", l.ToArray()); Console.WriteLine(str); } }

C#程序:打印整数列表中的重复项

Samual Sam
更新于 2020年6月22日 09:24:09

769 阅读量

要打印整数列表中的重复项,请使用ContainsKey。下面,我们首先设置整数:int[] arr = { 3, 6, 3, 8, 9, 2, 2 };然后使用Dictionary集合来获取重复整数的计数。让我们看看获取重复整数的代码:示例 在线演示using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 3, 6, ... 阅读更多

C#中的格式化字符串文字

karthikeya Boyini
更新于 2020年6月22日 09:26:44

454 阅读量

在 C# 中,使用 String.Format 方法来格式化字符串字面量。在下面的示例中,0 是对象的索引,其字符串值将插入到该特定位置 - using System; namespace Demo {    class Test {       static void Main(string[] args) {          decimal A = 15.2m;          string res = String.Format("Temperature = {0}°C.", A);          Console.WriteLine(res);       }    } }在下面的示例中,让我们为 double 类型格式化字符串。示例 在线演示using System; class Demo {    public static void Main(String[] args) { ... 阅读更多

广告
© . All rights reserved.