找到 34423 篇文章,关于编程

如何在 C# 中将十六进制字符串转换为十六进制数字?

karthikeya Boyini
更新于 2020年6月22日 13:04:03

2K+ 次浏览

首先,设置十六进制字符串 −string str = "7D";现在,使用 Convert.ToSByte() 方法将十六进制字符串转换为十六进制数字 −Console.WriteLine(Convert.ToSByte(str, 16));让我们看看完整的代码 −示例 在线演示using System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToSByte(str, 16));       }    } }输出125另一种将十六进制字符串转换为十六进制数字的方法 −示例 在线演示using System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToInt32(str, 16));       }    } }输出125

如何在 C# 中将元组转换为数组?

Samual Sam
更新于 2020年6月22日 12:53:45

836 次浏览

首先,设置一个元组 −Tuple t = Tuple.Create(99,53);现在,将元组转换为数组 −int[] arr = new int[]{t.Item1, t.Item2};以下是将元组转换为数组的代码 −示例 在线演示using System; using System.Linq; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          Tuple t = Tuple.Create(99,53);          int[] arr = new int[]{t.Item1, t.Item2};          foreach (int val in arr) {             Console.WriteLine(val);          }       }    }   }输出99 53

如何在 C# 中使用递归将十进制数转换为二进制数?

karthikeya Boyini
更新于 2020年6月22日 12:54:49

393 次浏览

要使用递归获得十进制的二进制值,首先设置十进制数 −int dec = 30;现在将值传递给函数 −public int displayBinary(int dec) { }现在,检查条件,直到十进制值为 0,并使用递归获取十进制数的模 2,如下所示。递归调用将使用 dec/2 值再次调用函数 −public int displayBinary(int dec) {    int res;    if (dec != 0) {       res = (dec % 2) + 10 * displayBinary(dec / 2);       Console.Write(res);       return ... 阅读更多

使用 C# 将两个已排序的数组合并到列表中

Samual Sam
更新于 2020年6月22日 12:55:32

218 次浏览

要将两个已排序的数组合并到列表中,首先设置两个已排序的数组 −int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };将其添加到列表并合并 −var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);       list.Add(array2[i]); }现在,使用 ToArray() 方法转换回数组,如下所示 −示例 在线演示using System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 56, 70, 77};       int[] array2 = ... 阅读更多

C# 程序,检查字符串是否包含所有元音

karthikeya Boyini
更新于 2020年6月22日 12:55:59

435 次浏览

要检查所有元音,首先设置检查条件 −string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();上面,我们使用了字符串 −string str = "the quick brown fox jumps over the lazy dog";现在,使用 Any() 方法检查字符串是否包含任何元音 −if(!res.Any()) Console.WriteLine("No vowels!");循环遍历字符串以获取元音 −示例 在线演示using System; using System.Linq; public class Program {    public static void Main() {       string str = "the quick brown fox jumps over the lazy dog";       var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();     ... 阅读更多

C# 程序,将列表中的所有数字相乘

Samual Sam
更新于 2020年6月22日 12:56:44

959 次浏览

首先,设置列表 −List myList = new List () {    5,    10,    7 };现在,将变量的值设置为 1,这将有助于我们进行乘法 −int prod = 1;循环遍历并获取乘积 −foreach(int i in myList) {    prod = prod*i; }以下是代码 −示例 在线演示using System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          5,          10,          7       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int prod = 1;       foreach(int i in myList) {          prod = prod*i;       }       Console.WriteLine("Product: {0}",prod);    } }输出List: 5 10 7 Product: 350

C++ 和 C# 中的 Foreach

karthikeya Boyini
更新于 2020年6月22日 12:57:28

607 次浏览

C++ 中的 ForeachC++ 11 引入了 foreach 循环来遍历每个元素。这是一个示例 −示例 在线演示#include using namespace std; int main() {    int myArr[] = { 99, 15, 67 };    // foreach 循环    for (int ele : myArr)    cout

在 C# 中打印 n 的前 m 个倍数

Samual Sam
更新于 2019年7月30日 22:30:23

583 次浏览

要打印 n 的 m 个倍数,首先设置 m 和 n 的值 − int n = 6, m = 1; 现在循环遍历 m 的值,递增它并在每次迭代中乘以 n − while (m

在 C# 中打印带有逗号作为千位分隔符的数字

Giri Raju
更新于 2020年6月22日 12:57:50

2K+ 次浏览

首先,将数字设置为字符串 −string num = "1000000.8765";现在,对小数点前后的数字进行不同的处理 −string withoutDecimals = num.Substring(0, num.IndexOf(".")); string withDecimals = num.Substring(num.IndexOf("."));使用 ToString() 方法设置 1000 分隔符的格式 −ToString("#,##0")以下是显示带有逗号作为千位分隔符的数字的完整代码 −示例 在线演示using System; public class Program {    public static void Main() {       string num = "1000000.8765";       string withoutDecimals = num.Substring(0, num.IndexOf("."));       string withDecimals = num.Substring(num.IndexOf("."));       withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");       Console.WriteLine(withoutDecimals + withDecimals);    } }输出1,000,000.8765

在 C# 中打印字符串中每个单词的首字母

usharani
更新于 2020年6月22日 12:58:16

1K+ 次浏览

假设字符串是 −string str = "Never Give Up!";首先,拆分每个单词 −string[] strSplit = str.Split();现在,循环遍历每个单词并使用子字符串方法显示首字母,如下面的代码所示 −示例 在线演示using System; public class Program {    public static void Main() {       string str = "Never Give Up!";       Console.WriteLine("Initial String= "+str);       Console.WriteLine("Displaying first letter of each word...");       string[] strSplit = str.Split();       foreach (string res in strSplit) {          Console.Write(res.Substring(0,1));       }    } }输出Initial String= Never Give Up! Displaying first letter of each word... NGU

上一页 1 ... 3160 3161 3162 3163 3164 ... 3443 下一页 (共3443页)
广告
© . All rights reserved.