简体   繁体   中英

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 ).

Lastly, I would use the variable as index with 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

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. 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:

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.

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

iConverted is instantiated (as null) to start, to allow an integer to also be null we have to use the ? as well

We TryParse the strUserInput into an Integer, and dump it into iConverted if successful (if not, iConverted remains null)

Finally, we print the requested index array, or notify the user that their index was out of bounds, or not properly parsed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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