如何从用户输入C#中填充数组?

从用户输入填充数组的最佳方法是什么?

解决方案是否会显示提示消息,然后从用户获取值?

string []answer = new string[10]; for(int i = 0;i 

你能澄清一下这个问题吗? 您是否尝试从用户那里获得固定数量的答案? 您期望什么数据类型 – 文本,整数,浮点十进制数? 这有很大的不同。

例如,如果你想要一个整数数组,你可以要求用户用空格或逗号分隔输入它们,然后使用

 string foo = Console.ReadLine(); string[] tokens = foo.Split(","); List nums = new List(); int oneNum; foreach(string s in tokens) { if(Int32.TryParse(s, out oneNum)) nums.Add(oneNum); } 

当然,你不一定要采取转换为整数的额外步骤,但我认为这可能有助于展示你的意愿。

将此作为arin代码的答案添加比在评论中继续这样做更有意义…

1)考虑使用decimal而不是double。 它更有可能给出用户期望的答案。 有关原因,请参见http://pobox.com/~skeet/csharp/floatingpoint.html和http://pobox.com/~skeet/csharp/decimal.html 。 小数据基本上更接近人类对数字的看法,而不是双倍。 Double更像计算机“自然地”思考数字,这就是为什么它更快 – 但这在这里并不相关。

2)对于用户输入,通常值得使用一种不会在错误输入上抛出exception的方法 – 例如decimal.TryParse和int.TryParse。 这些返回一个布尔值来表示解析是否成功,并使用out参数来给出结果。 如果你还没有开始学习参数,那么暂时忽略这一点可能是值得的。

3)这只是一点点,但我认为围绕所有“for”/“if”(等)身体进行括号是明智的,所以我要改变这个:

 for (int counter = 0; counter < 6; counter++) Console.WriteLine("{0,5}{1,8}", counter, array[counter]); 

对此:

 for (int counter = 0; counter < 6; counter++) { Console.WriteLine("{0,5}{1,8}", counter, array[counter]); } 

它使块更清晰,意味着你不小心写:

 for (int counter = 0; counter < 6; counter++) Console.WriteLine("{0,5}{1,8}", counter, array[counter]); Console.WriteLine("----"); // This isn't part of the for loop! 

4)你的switch语句没有default情况 - 所以如果用户键入“yes”或“no”以外的任何内容,它将忽略它们并退出。 你可能希望有类似的东西:

 bool keepGoing = true; while (keepGoing) { switch (answer) { case "yes": Console.WriteLine("==============================================="); Console.WriteLine("please enter the array index you wish to get the value of it"); int index = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("==============================================="); Console.WriteLine("The Value of the selected index is:"); Console.WriteLine(array[index]); keepGoing = false; break; case "no": Console.WriteLine("==============================================="); Console.WriteLine("HAVE A NICE DAY SIR"); keepGoing = false; break; default: Console.WriteLine("Sorry, I didn't understand that. Please enter yes or no"); break; } } 

5)当你开始学习LINQ时,你可能想回到这个并替换你的for循环,它将输入加总为:

 // Or decimal, of course, if you've made the earlier selected change double sum = input.Sum(); 

同样,这是相当先进的 - 现在不要担心它!

C#没有会收集输入的消息框,但您可以使用Visual Basic输入框。

如果您添加对“Microsoft Visual Basic .NET运行时”的引用,然后插入:

 using Microsoft.VisualBasic; 

您可以执行以下操作:

 List responses = new List(); string response = ""; while(!(response = Interaction.InputBox("Please enter your information", "Window Title", "Default Text", xPosition, yPosition)).equals("")) { responses.Add(response); } responses.ToArray(); 

尝试:

 array[i] = Convert.ToDouble(Console.Readline()); 

您可能还想使用double.TryParse()来确保用户没有输入伪造文本并以某种方式处理。

我已经完成了它最终检查它,如果有更好的方式告诉我们

  static void Main() { double[] array = new double[6]; Console.WriteLine("Please Sir Enter 6 Floating numbers"); for (int i = 0; i < 6; i++) { array[i] = Convert.ToDouble(Console.ReadLine()); } double sum = 0; foreach (double d in array) { sum += d; } double average = sum / 6; Console.WriteLine("==============================================="); Console.WriteLine("The Values you've entered are"); Console.WriteLine("{0}{1,8}", "index", "value"); for (int counter = 0; counter < 6; counter++) Console.WriteLine("{0,5}{1,8}", counter, array[counter]); Console.WriteLine("==============================================="); Console.WriteLine("The average is ;"); Console.WriteLine(average); Console.WriteLine("==============================================="); Console.WriteLine("would you like to search for a certain elemnt ? (enter yes or no)"); string answer = Console.ReadLine(); switch (answer) { case "yes": Console.WriteLine("==============================================="); Console.WriteLine("please enter the array index you wish to get the value of it"); int index = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("==============================================="); Console.WriteLine("The Value of the selected index is:"); Console.WriteLine(array[index]); break; case "no": Console.WriteLine("==============================================="); Console.WriteLine("HAVE A NICE DAY SIR"); break; } } 

将输入值添加到List中,完成后使用List.ToArray()获取包含值的数组。

当然…. Console.ReadLine总是返回字符串….所以你必须将类型字符串转换为double

arrays[I] = double.Parse(到Console.ReadLine());

readline是for string ..只需使用read