[英]How to individually assign a value from console to an array
I am making a program that continually prompts the user to input numbers which are then stored in an array until the user provides a given response (Eg "continue"), where the program then displays the array in terminal. 我正在制作一个程序,该程序不断提示用户输入数字,然后将其存储在数组中,直到用户提供给定的响应(例如“ continue”)为止,然后程序在终端中显示该数组。
I have been debugging my program and it is setting all values within the array to the last value from console input. 我一直在调试程序,它正在将数组中的所有值设置为控制台输入中的最后一个值。
How can I stop the counter from going all the way to the max for that array and instead step through the array 1 by 1 and input a new value from console each time 我如何才能停止计数器一直到达该数组的最大值,而是一步一步地遍历该数组并每次从控制台输入一个新值
for (int i = 0; i < inputArray.GetLength(0); i++)
inputArray[i] = Convert.ToDouble(input);
goto STARTOVER;
Here is my main code: 这是我的主要代码:
using System;
namespace Task3
{
class MainClass
{
public static void Main(string[] args)
{
double[] inputArray = new double[40];
Console.WriteLine("Begin");
while (true)
{
STARTOVER:
var input = Console.ReadLine();
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
if (input.Equals("continue",StringComparison.OrdinalIgnoreCase))
{
goto DISPLAY;
}
else
{
for (int i = 0; i < inputArray.GetLength(0); i++)
inputArray[i] = Convert.ToDouble(input);
goto STARTOVER;
}
}
DISPLAY:
for (int i = 0; i < inputArray.GetLength(0); i++)
{
Console.Write("{0} ", inputArray[i]);
Console.WriteLine();
}
}
}
}
From my understanding, I shouldn't use goto where possible but I cannot find an alternative to get the program to go back to where "STARTOVER:" is. 据我了解,我不应该在可能的地方使用goto,但是我找不到替代方法来使程序返回到“ STARTOVER:”所在的位置。
Aside from goto
, which is pretty bad, the problem is the code loops through the entire array on every entry. 除了
goto
,这很糟糕,问题在于代码在每个条目上遍历整个数组。 It needs an integer variable that starts at 0 and increments with each new input, so it can set just that position each time. 它需要一个从0开始并随每个新输入递增的整数变量,因此每次只能设置该位置。
public static void Main(string[] args)
{
Console.WriteLine("Begin");
double[] inputArray = new double[40];
int inputCount = 0;
var input = Console.ReadLine();
while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase)
&& !input.Equals("continue", StringComparison.OrdinalIgnoreCase)
&& inputCount < inputArray.Length)
{
inputArray[inputCount++] = Convert.ToDouble(input);
input = Console.ReadLine();
}
for (int i = 0; i < inputCount; i++)
{
Console.Write("{0} ", inputArray[i]);
Console.WriteLine();
}
}
Even better to use a List: 使用List更好:
public static void Main(string[] args)
{
Console.WriteLine("Begin");
var data = new List<double>;
var input = Console.ReadLine();
while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase)
&& !input.Equals("continue", StringComparison.OrdinalIgnoreCase))
{
data.Add(double.Parse((input));
input = Console.ReadLine();
}
for (double d in data)
{
Console.WriteLine(d);
}
}
The key to repeatedly requesting input while a condition is met, is using a while loop 在满足条件时重复请求输入的关键是使用while循环
while(CurrentCondition != PassCondition)
{
//do something, in this case ask for user input
}
a good idea would be to create methods, and call these methods where the goto methods are eg 一个好主意是创建方法,并在goto方法为例如的地方调用这些方法
public static double[] inputArray = new double[40];
public static string input;
public static int idx = 0;
static void Main(string[] args)
{
GetInput();
Console.Read();
}
public static void GetInput()
{
Console.Write("enter a number to add to the array or 'continue' to display the array: ");
input = Console.ReadLine();
while (!CheckInput(input))
{
Console.Write("enter a number to add to the array or 'continue' to display the array: ");
input = Console.ReadLine();
}
}
public static bool CheckInput(string _input)
{
bool Continue = false;
if (input == "continue")
{
PrintArray();
Continue = true;
}
else
{
inputArray[idx] = double.Parse(_input);
idx++;
Continue = false;
}
return Continue;
}
public static void PrintArray()
{
for(int i = 0; i < inputArray.Length - 1; i++)
{
Console.WriteLine(inputArray[i]);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.