C# C以相反的顺序打印字符串数组
所以我有一个问题,当我打开我的程序并选择4时,应该让它显示数组中的所有字符串,但相反C# C以相反的顺序打印字符串数组,c#,C#,所以我有一个问题,当我打开我的程序并选择4时,应该让它显示数组中的所有字符串,但相反 using System.Linq; using System.Diagnostics; using System.Runtime.CompilerServices; namespace Homework3 { class Program { static void Main(string[] args) { int optionIntO
using System.Linq;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Homework3
{
class Program
{
static void Main(string[] args)
{
int optionIntOne;
int optionIntTwo;
int i;
int num = 0;
decimal k;
string[] names = {"Keyhan", "Adolf", "Niklas", "Jenny",
"Elisa", "Rudolf", "Bengt", "Hassan", "Kajsa", "Maggan"};
Console.WriteLine("Please Select an Option Below:\n1 - Find a student by Index \n" +
"2 - Find student Index by name\n3 - Display all student names with their Index \n" +
"4 - Display all the students in reverse order");
string optionStringone = Console.ReadLine();
bool isNumericOne = Int32.TryParse(optionStringone, out optionIntOne);
Console.Clear();
switch (optionIntOne)
{
case 1:
Console.WriteLine("***FIND Student by ID" + "\n" + "Please select a number between 0 - 9:");
string studentNumber = (Console.ReadLine());
bool isNumericTwo = Int32.TryParse(studentNumber, out optionIntTwo);
if (isNumericTwo == true && optionIntTwo < names.Length)
Console.WriteLine(names[optionIntTwo]);
else
Console.WriteLine("Please enter a valid number");
break;
case 2:
Console.WriteLine("*** FIND Student Index by Name \n Please Print one of the above names");
foreach (string name in names)
Console.WriteLine((name));
Console.WriteLine("\n");
string studentName = (Console.ReadLine().ToUpper());
//ToUpper så slipper loopen göra det på repeat
Console.Clear();
bool b = false;
if (decimal.TryParse(studentName, out k) || studentName == string.Empty)
{
Console.WriteLine("Please Enter a Valid Name");
}
else
{
for (i = 0; i < names.Length; i++)
{
if (names[i].ToUpper() == studentName)
{
b = true;
Console.WriteLine(names[i] + " Has the index of " + i);
break;
}
}
if (b == false)
Console.WriteLine("The Student does not Exist in the List");
}
break;
case 3:
while (num < names.Length)
{
Console.WriteLine(num + " - " + names[num]);
num++;
}
break;
case 4:
while (num < names.Length)
{
Console.WriteLine(names[num].Reverse());
num++;
}
break;
default:
Console.WriteLine("Enter a Valid Number");
break;
}
Console.ReadKey();
}
}
}
选项3,但按相反顺序打印
VisualStudio没有给我任何错误,但它的输出是这样的,而不是相反的顺序
有人知道如何解决吗?/可以指出问题所在首先:
Console.WriteLine(names[num].Reverse());
正在反转列表中的字符串,因此名称[0]。反转将是nahyeK,因为字符串也是一个列表或字符列表。如果你想推翻整个名单,你只写名字;
更多信息请访问:
所以下一步就是把它打印出来。
当你做列表的时候,反转;它将返回IEnumerable,您可以在循环中使用它来获取名称
这里有一个选择:
static void Main(string[] args)
{
int num = 0;
string[] names = { "Alice", "Bob", "Charles", "Delilah" };
while (num < names.Length)
{
Console.WriteLine(names.Reverse().ElementAt(num));
num++;
}
// Prints
//
// Delilah
// Charles
// Bob
// Alice
Console.ReadKey();
}
作为问题的一部分,发布带有完整回溯的代码和错误;不作为图像或链接到外部网站。欢迎使用堆栈溢出!请在你的问题中选择、阅读和提供,而不是作为外部链接,对于int i=names.Length-1;i>=0;i-{Console.WriteLinenames[i];}嘿,ElementAt是如何工作的?很好,谢谢!您也可以只执行从names.Length-1到0的for循环,而不是反转列表。@Someone123。ElementAt返回序列中指定索引处的元素。在你的例子中,我们首先反转数组,然后要求索引0 ElementAt0,索引1 ElementAt1等等@AlexP啊,好的,我现在明白了。谢谢@有人123下次如果你有问题,比如如何工作,我真的鼓励你只阅读文档。这回答了很多问题,甚至在他们需要被问之前。很好,你采纳了这个建议。大多数低质量的答案没有得到改善,这让我们对评论感到愤怒。欢迎使用Stack Overflow并继续学习该系统!谢谢仅供参考,您根本不需要ToList-从Reverse返回的IEnumerable在foreach循环中运行良好。尽管如果您想在一行中完成所有操作,您需要这样做:names.Reverse.ToList.ForEachConsole.WriteLine@谢谢你的评论!我更新了我的答案。
foreach(name in names.Reverse().ToList())
{
Console.WriteLine(name);
}
static void Main(string[] args)
{
int num = 0;
string[] names = { "Alice", "Bob", "Charles", "Delilah" };
while (num < names.Length)
{
Console.WriteLine(names.Reverse().ElementAt(num));
num++;
}
// Prints
//
// Delilah
// Charles
// Bob
// Alice
Console.ReadKey();
}
static void Main(string[] args)
{
string[] names = { "Alice", "Bob", "Charles", "Delilah" };
for (int num = names.Length - 1; num >= 0; num--)
{
Console.WriteLine(names[num]);
}
Console.ReadKey();
}