[英]How do I insert elements into an array C#
I'm new to programming and for one of my college classes I have to use C#.我是编程新手,对于我的一门大学课程,我必须使用 C#。 I'm not super familiar with C# and I am having trouble with my assignment.
我对 C# 不是很熟悉,而且我的作业有问题。 And unfortunatly I've spent a little over an hour trying to find solutions.
不幸的是,我花了一个多小时试图找到解决方案。 And my search has come up empty.
我的搜索结果空空如也。 No one I know understands or can code anything.
我认识的人没有人理解或可以编写任何代码。 Plz Help
请帮助
Under the Main method, create an array named as n of 10 integers.
在 Main 方法下,创建一个名为 n 的 10 个整数的数组。
Create a for loop to assign the value to the n array from 100 to 109.
创建一个 for 循环,将值从 100 到 109 分配给 n 数组。
Create a foreach loop to output each array element's value.
创建一个 foreach 循环来输出每个数组元素的值。
static void Main(string[] args)
{
int[] n = new int[10];
for (int i= 0; i < 10; i++)
{
n[i] = 100 + i;
}
foreach (int i in n)
{
Console.WriteLine(int[i]);
}
}
This line:这一行:
Console.WriteLine(int[i]);
Must be replaced with this:必须用这个替换:
Console.WriteLine(i);
Your code is almost perfect for the assignment你的代码几乎完美的分配
static void Main(string[] args)
{
// create an array named as n of 10 integers
int[] n = new int[10];
// create a for loop to assign the value to the n array from 100 to 109.
for (int i= 0; i < 10; i++)
{
n[i] = 100 + i;
}
// create a foreach loop to output each array element's value.
foreach (int item in n)
{
Console.WriteLine(item);
}
}
as you can see, in your code i
is an integer from the array element, and not the array index.如您所见,在您的代码中,
i
是来自数组元素的整数,而不是数组索引。 So your int[i]
is incorrect syntax.所以你的
int[i]
是不正确的语法。
I implemented a few alternatives as well as a solution to your immediate challenge.我实施了一些替代方案以及解决您当前面临的挑战的解决方案。 In
int[i]
you wrote down the data type int
instead of the name of the array, which is n
.在
int[i]
您写下了数据类型int
而不是数组的名称,即n
。 when i
is a valid index n[i]
gives you the value.当
i
是一个有效的索引时n[i]
给你值。 in a foreach
construct you give the value a name to be used in each iteration.在
foreach
构造中,您可以为值指定一个在每次迭代中使用的名称。 confusingly you named it i
as well, so i
gives you the value.令人困惑的是,你也将它命名为
i
,所以i
给了你这个值。 i
is common in for
loops as the index so this may be confused with that idiom and you would be better off using a more speaking name like number
in your code. i
在for
循环中作为索引很常见,因此这可能会与该习惯用法混淆,并且您最好在代码中使用更容易理解的名称,例如number
。
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Implementation1();
Console.WriteLine();
Implementation2();
Console.WriteLine();
Implementation3();
}
static void Implementation1()
{
int[] n = new int[10];
for (int i = 0; i < n.Length; ++i)
n[i] = 100 + i;
for (int i = 0; i < n.Length; ++i)
Console.WriteLine(n[i]);
}
static void Implementation2()
{
int[] n = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < n.Length; ++i)
n[i] = 100 + i;
foreach (var number in n)
Console.WriteLine(number);
}
static void Implementation3()
{
var n = Enumerable.Range(100, 10).ToArray();
string.Join(',', n.Select(val => $"{val}"));
Console.WriteLine(string.Join(',', n.Select(val => $"{val}")));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.