从控制台询问一个单词并打印数组中每个字母的索引
ask a word from the console and print the index of each of its letters in the array
我需要编写一个 C# 程序来创建一个包含字母表中所有字母的数组,并从控制台请求一个单词并打印数组中每个字母的索引。(我不要求整个代码我只需要帮助我该怎么做,谢谢)
这就是我所做的;
using System.Collections.Generic;
using System.Linq;
namespace Letters
{
public class letters
{
public static void Main(string[] args)
{
int l = 0;
string[] letters = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
List<string> letterList = letters.ToList();
Console.Write("Enter a word: ");
string word = Console.ReadLine();
while (l <= word.Length - 1)
{
Console.WriteLine(letterList.IndexOf(l)); l++;
}
}
}
}
但我需要这样的输出
输入一个词:词
w:23
o:14
r:18
d:3
关闭 - 这是更正和简化的版本
public static void Main(string[] args) {
int l = 0;
char[] letters = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
Console.Write("Enter a word: ");
string word = Console.ReadLine();
foreach(char ch in word) {
Console.WriteLine(Array.IndexOf(letters,ch) + 1);
}
}
为了好玩,这里有一个衬垫
Console.WriteLine(word.Select(ch => $"{ch}:{(Char.ToLower(ch) - 'a') + 1} ").Aggregate((a, b) => a + b));
使用 Flydogs 计算而不是查找 table
我需要编写一个 C# 程序来创建一个包含字母表中所有字母的数组,并从控制台请求一个单词并打印数组中每个字母的索引。(我不要求整个代码我只需要帮助我该怎么做,谢谢)
这就是我所做的;
using System.Collections.Generic;
using System.Linq;
namespace Letters
{
public class letters
{
public static void Main(string[] args)
{
int l = 0;
string[] letters = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
List<string> letterList = letters.ToList();
Console.Write("Enter a word: ");
string word = Console.ReadLine();
while (l <= word.Length - 1)
{
Console.WriteLine(letterList.IndexOf(l)); l++;
}
}
}
}
但我需要这样的输出
输入一个词:词
w:23 o:14 r:18 d:3
关闭 - 这是更正和简化的版本
public static void Main(string[] args) {
int l = 0;
char[] letters = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
Console.Write("Enter a word: ");
string word = Console.ReadLine();
foreach(char ch in word) {
Console.WriteLine(Array.IndexOf(letters,ch) + 1);
}
}
为了好玩,这里有一个衬垫
Console.WriteLine(word.Select(ch => $"{ch}:{(Char.ToLower(ch) - 'a') + 1} ").Aggregate((a, b) => a + b));
使用 Flydogs 计算而不是查找 table