如何用用户给出的索引和值替换double,并将每个元素转移到右边并删除最后一个元素

发布于 2025-01-28 13:19:41 字数 446 浏览 1 评论 0原文

我不确定要使用哪种功能或如何使用它,我有一个替换值的问题。

问题:创建和填充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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2025-02-04 13:19:41

您明确地说这是一项任务(在我看来,只要您这么说,这是可以的)。但是,我不喜欢您的作业;如果我这样做,您将不会学到任何东西。相反,我将帮助您弄清楚。首先,您的代码:

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;

现在更明确地分配:

  • 创建和填充5个双打的数组。
  • 要求用户输入一个值“ V”和索引“ I”。
  • 您的程序应用数组中的索引“ i”的值替换为“ v”,
  • 将每个元素转移到右侧并删除最后一个元素。

您已经完成了前两个部分。

您需要做的是交换最后两个部分 - 首先为新元素腾出空间,然后,一旦有一个新值的地方,请将其放入。

要为新价值腾出空间,您有两种情况:

  • 用户选择数组中的最后一个索引。在这种情况下,您只需覆盖最后一个元素,并且您的工作完成了
  • 用户选择其他任何索引(0..N-2)。在这种情况下,您需要做的是将数组从所选索引循环到数组的结尾(少于一个),从i th输入到i+1 TH条目。当您这样做时,您可能会发现为什么大多数程序员认为“逐一”问题是最常见的错误。
    • 不过请考虑一下。您需要循环向后(即,从数组的末端回到所选索引)。如果您在正常 forwards 方向上执行此操作,那么您将在创建任何空间之前覆盖事物

,然后覆盖适当的数组输入。

其他一些注释:

  • 您将数组键入double [],命名myDoublearray并使用int s初始化。您与用户的所有交互也使用int s。我意识到INT可以隐式转换为双打,但是您应该真正决定要做什么并始终如一地做到这一点。 Double 文字只是一个具有小数点的数字,例如3.14152
  • 您需要确定是否要编号索引0..N-1(c#喜欢)或1..n(人类喜欢)。在您的程序顶部写下有关此权利的提示。类似:
System.Console.WriteLine($"There is an an array with {myDoubleArray.Length} doubles in it, indexed 1 to {myDoubleArray.Length}.");
  • 您的代码的方式,如果用户输入不正确的格式值,则您的程序将引发异常。用户脂肪的数据输入几乎不是例外行为。考虑使用int.tryparsedouble.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:

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;

Now the assignment, more explicitly:

  • 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’
  • shifting each element to the right and dropping the last element.

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:

  • The user picks the last index in the array. In this case, you just overwrite the last element and your work is done
  • The user picks any other index (0..N-2). In that case, what you need to do is to loop over the array from the selected index to end of the array (less one), copying from the ith entry to the i+1th 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.
    • Think about this though. You need to loop backwards (i.e. from the end of the array back towards the selected index). If you do it in the normal forwards direction, then you will be overwriting things before you have created any space

Once you have "created some space", then overwrite the appropriate array entry.

Some other comments:

  • You type your array as double[], name it myDoubleArray and initialize it with ints. All your interactions with your users use ints 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 like 3.14152
  • You need to decide if you are going to number your indexes 0..N-1 (which C# likes) or 1..N (which humans like). Write out a hint about this right at the top of your program. Something like:
System.Console.WriteLine(
quot;There is an an array with {myDoubleArray.Length} doubles in it, indexed 1 to {myDoubleArray.Length}.");
  • The way you have your code, if a user enters an incorrectly formatted value, your program will throw an exception. A user fat-fingering data entry is hardly exceptional behavior. Consider using int.TryParse and double.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文