简体   繁体   English

c#控制台从用户输入获取带有索引的数组值

[英]c# console get array value with index from user input

I'm in an intro to programming class.我正在学习编程课程。 Is it possible to output an array value when a user selects its index?当用户选择其索引时是否可以输出数组值? Its not much but this is my code so far:它并不多,但这是我到目前为止的代码:

double[] cisTuition = new double[] { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");

You need to access using index of the array您需要使用数组的索引进行访问

Console.WriteLine(cisTuition[index]);

For example, if you need to get the index from the user entered input value,例如,如果您需要从用户输入的输入值中获取索引,

int input = int.Parse(Console.ReadLine());
Console.WriteLine("value is: " + cisTuition[input]);

Yes, you can reach an value inside and array at a particular index position as below:是的,您可以在特定索引位置到达数组中的值,如下所示:

double[] cisTuition = new double[] { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine(cisTuition[0]);

Ouput:输出:

0.00

Then, as you requested to have an index provided by an input, I would use Console.ReadLine() to get the user choice and record it into a variable ( index ).然后,当您请求通过输入提供索引时,我将使用Console.ReadLine()获取用户选择并将其记录到变量 ( index ) 中。

Lastly, I would use the variable as index with cisTuition[index] .最后,我会将该变量用作cisTuition[index]

See the full code below:请参阅下面的完整代码:

double[] cisTuition = new double[] { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Enter input:"); // Prompt the question
string index = Console.ReadLine(); //  Record the input
Console.WriteLine(cisTuition[index]); // Show the array value of this index

Yes, you can get the value of the cisTuition array by their index: Check the snippet below是的,您可以通过索引获取cisTuition数组的值:检查下面的片段

double[] cisTuition = new double[] { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");

int index = int.Parse(Console.ReadLine());
Console.WriteLine("value is: " + cisTuition[index]);

Of course, you can improve that snippet validating that the index is inside the bounds of the array, but that's the basic idea.当然,您可以改进该片段以验证索引是否在数组的边界内,但这是基本思想。

    bool Keeplooping = true; //Boolean to tell whether the loops continues
    while (Keeplooping == true) //while the user hasn't chosen a valid index
    {
    Console.WriteLine("Select an index");
    try //if this fails then the input is not an int or too big/small
    {
    int index = int.Parse(Console.ReadLine()); //receive input
    Console.WriteLine(cisTuition[index].ToString()); //output the value
    Keeplooping = false; //loop will end after this iteration
    }
    catch //alerts user that the input is bad and tries again
    {
        Console.WriteLine("Please select a valid index");
    }
    }

This should do the trick (I tested it already to be sure)这应该可以解决问题(我已经测试过了)

Well first you need the users input, when you got this you can do your proper checks, like is it NaN, or if it's outside the arrays index.那么首先你需要用户输入,当你得到这个时你可以做你正确的检查,比如它是 NaN,或者它是否在数组索引之外。 After this it should be easy for look out the index in the array and then print it.在此之后,应该很容易查看数组中的索引然后打印它。 I dont program that much in C#, but i guess it could look something like:我不怎么用 C# 编程,但我想它可能看起来像:

double[] cisTuition = new double[] { 0.00, 1.00, 1.50, 2.00, 2.50 };
Console.WriteLine("Please choose the semester");

var UserInput = getUserInput();

if(UserInput == NaN || UserInput > cisTuition.length)
return()

print( cisTuition[UserInput] )

I know this is definitely not the correct syntax, but the logic should work.我知道这绝对不是正确的语法,但逻辑应该有效。 Hope it helps.希望能帮助到你。

I think what is being missed here is how to read in a value, convert it to an int if possible, and then use that as the reference to the array index.我认为这里遗漏的是如何读入一个值,如果可能将其转换为int ,然后将其用作对数组索引的引用。

So, if that's the case, you'd want something akin to this added to the end of your code:因此,如果是这种情况,您可能希望在代码末尾添加类似于以下内容的内容:

string strUserInput = Console.ReadLine();
int? iConverted = null;
int.TryParse(strUserInput, out iConverted);
if ((iConverted != null) && (iConverted <= (cisTuition.Length - 1)) )
{
  Console.WriteLine(cisTuition[iConverted]);
}
else
{
  Console.WriteLine("Invalid value or index of the array");
}

Full explanation:完整解释:

ReadLine() pulls in the value the user provides as the index to the array ReadLine()拉入用户提供的值作为数组的索引

iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? iConverted被实例化(作为空)开始,为了允许一个整数也为空,我们必须使用? as well还有

We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)我们TryParsestrUserInput成整数,并倾倒入iConverted如果成功的话(如果没有, iConverted保持为空)

Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.最后,我们打印请求的索引数组,或者通知用户他们的索引超出范围,或者没有正确解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM