C# 用户更改数组中的值
我是C#的新手,在过去的一个小时里,我一直在努力寻找一个如何提供帮助的教程 用户更改数组中任何元素的值的一种方法,它需要确保索引有效。你能帮我吗,或者给我提供一些信息,这样我就可以学习了。我似乎找不到它,至少没有什么简单的东西,因为我还是编程新手 下面提供了我将使用的代码示例 澄清:用户输入值用他们选择的一个索引值进行切换C# 用户更改数组中的值,c#,.net,arrays,methods,C#,.net,Arrays,Methods,我是C#的新手,在过去的一个小时里,我一直在努力寻找一个如何提供帮助的教程 用户更改数组中任何元素的值的一种方法,它需要确保索引有效。你能帮我吗,或者给我提供一些信息,这样我就可以学习了。我似乎找不到它,至少没有什么简单的东西,因为我还是编程新手 下面提供了我将使用的代码示例 澄清:用户输入值用他们选择的一个索引值进行切换 int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };//<<< values Change
int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };//<<< values
ChangedArray(A);
//Method down here returning value on top to display array
static void ChangeArray(int[] array)
{
Console.WriteLine("\n=============\n");
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}", array[i]);
}
Console.WriteLine("\n=============\n");
}
int[]A={-2,5,1,9,6,23,67,1,8,7,3,90}// 这假设用户输入有效
您可以为数组编制索引,然后简单地分配值
static void ChangeArray(int[] array)
{
int index = Convert.ToInt32(Console.ReadLine());
int newValue= Convert.ToInt32(Console.ReadLine());
if(index <= array.Length && index >= 0)
{
array[index] = newValue;
}
Console.WriteLine("\n=============\n");
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}", array[i]);
}
Console.WriteLine("\n=============\n");
}
静态void ChangeArray(int[]数组)
{
int index=Convert.ToInt32(Console.ReadLine());
int newValue=Convert.ToInt32(Console.ReadLine());
如果(索引=0)
{
数组[index]=newValue;
}
Console.WriteLine(“\n======================\n”);
for(int i=0;i
这假定用户输入有效
您可以为数组编制索引,然后简单地分配值
static void ChangeArray(int[] array)
{
int index = Convert.ToInt32(Console.ReadLine());
int newValue= Convert.ToInt32(Console.ReadLine());
if(index <= array.Length && index >= 0)
{
array[index] = newValue;
}
Console.WriteLine("\n=============\n");
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0}", array[i]);
}
Console.WriteLine("\n=============\n");
}
静态void ChangeArray(int[]数组)
{
int index=Convert.ToInt32(Console.ReadLine());
int newValue=Convert.ToInt32(Console.ReadLine());
如果(索引=0)
{
数组[索引]=新值;
}
Console.WriteLine(“\n======================\n”);
for(int i=0;i
类似的内容:
public void Main(...)
{
Try
{
int[] myArray = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };
int index = Convert.ToInt32(Console.ReadLine())
int newValue = Convert.ToInt32(Console.ReadLine())
ChangeArray(myArray,index, newValue);
}
Catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void ChangeArray(int[] array, int index, int newValue )
{
if (array.Length >= index || index < 0)
{
Console.WriteLine("\n=====No change========\n");
return;
}
Console.WriteLine("\n=====Old values========\n");
for (int i = 0; i < array.Length; i++)
Console.Write("{0}", array[i]);
array[index] = newValue;
Console.WriteLine("\n\n======New values=======\n");
for (int i = 0; i < array.Length; i++)
Console.Write("{0}", array[i]);
}
public void Main(…)
{
尝试
{
int[]myArray={-2,5,1,9,6,23,67,1,8,7,3,90};
int index=Convert.ToInt32(Console.ReadLine())
int newValue=Convert.ToInt32(Console.ReadLine())
ChangeArray(myArray、index、newValue);
}
捕获(例外e)
{
控制台写入线(e.Message);
}
}
静态void ChangeArray(int[]数组、int索引、int newValue)
{
if(array.Length>=索引| |索引<0)
{
Console.WriteLine(“\n==No change==;
返回;
}
Console.WriteLine(“\n=Old values===========\n”);
for(int i=0;i
类似的内容:
public void Main(...)
{
Try
{
int[] myArray = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };
int index = Convert.ToInt32(Console.ReadLine())
int newValue = Convert.ToInt32(Console.ReadLine())
ChangeArray(myArray,index, newValue);
}
Catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void ChangeArray(int[] array, int index, int newValue )
{
if (array.Length >= index || index < 0)
{
Console.WriteLine("\n=====No change========\n");
return;
}
Console.WriteLine("\n=====Old values========\n");
for (int i = 0; i < array.Length; i++)
Console.Write("{0}", array[i]);
array[index] = newValue;
Console.WriteLine("\n\n======New values=======\n");
for (int i = 0; i < array.Length; i++)
Console.Write("{0}", array[i]);
}
public void Main(…)
{
尝试
{
int[]myArray={-2,5,1,9,6,23,67,1,8,7,3,90};
int index=Convert.ToInt32(Console.ReadLine())
int newValue=Convert.ToInt32(Console.ReadLine())
ChangeArray(myArray、index、newValue);
}
捕获(例外e)
{
控制台写入线(e.Message);
}
}
静态void ChangeArray(int[]数组、int索引、int newValue)
{
if(array.Length>=索引| |索引<0)
{
Console.WriteLine(“\n==No change==;
返回;
}
Console.WriteLine(“\n=Old values===========\n”);
for(int i=0;i
这假定控制台的输入有效,并且范围内有一个int[]A
。我假设这就是您想要它的地方,因为您使用的控制台方法与此不同
Console.Write("Index to edit:");
int indexToChange = Convert.ToInt32(Console.ReadLine());
Console.Write("To value:");
int valueToSave = Convert.ToInt32(Console.ReadLine());
A[indexToChange] = valueToSave;
这假定控制台的输入有效,范围内有int[]A
。我假设这就是您想要它的地方,因为您使用的控制台方法与此不同
Console.Write("Index to edit:");
int indexToChange = Convert.ToInt32(Console.ReadLine());
Console.Write("To value:");
int valueToSave = Convert.ToInt32(Console.ReadLine());
A[indexToChange] = valueToSave;
您如何检测用户想要更改的值?他们想利用什么价值?现在还不清楚问题出在哪里。您是否询问如何接收用户输入?如何设置值?您是否尝试了某项操作并收到错误或观察到意外行为?接收用户输入,然后切换他们选择的索引,并使用用户输入切换该索引的值。您的调用函数名和实际函数名不同。请将您的问题记录下来。您如何检测用户想要的值改变他们想利用什么价值?现在还不清楚问题出在哪里。您是否询问如何接收用户输入?如何设置值?您是否尝试了某些操作并收到错误或观察到意外行为?接收用户输入,然后切换他们选择的索引,并使用用户输入切换该索引的值。您的调用函数名和实际函数名不同。请记下您的问题。@TheBoringGuy haha抱歉that@TheBoringGuy哈哈对不起