如何用用户给出的索引和值替换double,并将每个元素转移到右边并删除最后一个元素
我不确定要使用哪种功能或如何使用它,我有一个替换值的问题。
问题:创建和填充5个双打的数组。要求用户输入一个值“ V”和索引“ I”。您的程序应用“ v”中的索引“ i”的值替换为“ v”,同时将每个元素转移到右侧并删除最后一个元素。
这是我到目前为止所做的,但尚未起作用
double[] myDoubleArray = {1, 2, 3, 4, 5};
System.Console.Write(" Please input a value: ");
int v = Convert.ToInt32(Console.ReadLine());
System.Console.Write(" Please input an index value: ");
int i = Convert.ToInt32(Console.ReadLine());
myDoubleArray[i] = v;
I have a problem on replacing the value as I'm not sure which function to use or how to use it.
The question: Create and populate an array of 5 doubles. Ask the user to input a value ‘v’ and an index ‘i’. Your program should replace the value at index ‘i’ in the array with the value ‘v’ while shifting each element to the right and dropping the last element.
This is what I've done so far but it hasn't worked
double[] myDoubleArray = {1, 2, 3, 4, 5};
System.Console.Write(" Please input a value: ");
int v = Convert.ToInt32(Console.ReadLine());
System.Console.Write(" Please input an index value: ");
int i = Convert.ToInt32(Console.ReadLine());
myDoubleArray[i] = v;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您明确地说这是一项任务(在我看来,只要您这么说,这是可以的)。但是,我不喜欢做您的作业;如果我这样做,您将不会学到任何东西。相反,我将帮助您弄清楚。首先,您的代码:
现在更明确地分配:
您已经完成了前两个部分。
您需要做的是交换最后两个部分 - 首先为新元素腾出空间,然后,一旦有一个新值的地方,请将其放入。
要为新价值腾出空间,您有两种情况:
i
th输入到i+1
TH条目。当您这样做时,您可能会发现为什么大多数程序员认为“逐一”问题是最常见的错误。,然后覆盖适当的数组输入。
其他一些注释:
double []
,命名myDoublearray
并使用int
s初始化。您与用户的所有交互也使用int
s。我意识到INT可以隐式转换为双打,但是您应该真正决定要做什么并始终如一地做到这一点。 Double 文字只是一个具有小数点的数字,例如3.14152
int.tryparse
和double.tryparse
。它们允许您在进行转换之前测试良好的价值。这样,如果用户输入不良数据,您可以重复提示,直到他/她输入好数据为止。享受
You are explicitly saying that this is an assignment (which, in my mind, is OK, as long as you say so). However, I don't like doing your assignments; if I do it, you won't learn anything. Instead, I'm going to help you figure it out. First your code:
Now the assignment, more explicitly:
You have done the first two parts.
What you need to do is do swap the last two pieces - first make room for the new element, and then, once there is a place for the new value, put it in.
To make room for the new value, you have two cases:
i
th entry to thei+1
th entry. While you are doing this, you will likely find out why most programmers think that "off-by-one" issues are the most common bugs there are.Once you have "created some space", then overwrite the appropriate array entry.
Some other comments:
double[]
, name itmyDoubleArray
and initialize it withint
s. All your interactions with your users useint
s as well. I realize that ints can be implicitly converted to doubles, but you should really decide what you want to do and do it consistently. A double literal is just a number with a decimal point like3.14152
int.TryParse
anddouble.TryParse
. They allow you to test for a good value before doing the conversion. That way if the user enters bad data, you can repeat the prompt until he/she enters good data.Enjoy