April 28th, 2010, 01:46 AM
#1
Simple C# Question
I have a simple c# question about how to grab a users input and store it into an Array
public void RunExercise()
{
int[] userIn = new int[9];
Console.WriteLine("User, please enter number 0 - 10");
}
This is what I have so far...
I just want it to ask the user to enter numbers 0-10
and then store them in the array
and then just sum up the numbers and print it out...
If anyone could help me that would be much appreciated
April 28th, 2010, 01:49 AM
#2
Re: Simple C# Question
Check out the Console.ReadLine method
It's not a bug, it's a feature!
April 28th, 2010, 01:55 AM
#3
Re: Simple C# Question
I looked at those before and I know how it works with normal int and strings. I just don't know how to use it when it comes to arrays
April 28th, 2010, 02:03 AM
#4
Re: Simple C# Question
Then you need to figure out how to modify an array, which really has nothing to do with gathering user input.
I also just noticed that your array has a Length of 9, but you're asking the user to enter 10 numbers. See a problem?
To learn more about arrays check this out.
It's not a bug, it's a feature!
April 28th, 2010, 09:41 AM
#5
Re: Simple C# Question
just something simple...
Code:
class Program
{
static void Main(string[] args)
{
int[] array = RunExercise();
Console.WriteLine("You entered numbers: ");
foreach (int i in array)
{
Console.WriteLine(i);
}
Console.Read();
}
public static int[] RunExercise()
{
int[] userIn = new int[9];
int counter = 0;
while (counter < 9)
{
Console.WriteLine("User, please enter number 0 - 10 [{0}]", counter + 1);
userIn[counter] = int.Parse(Console.ReadLine());
counter++;
}
return userIn;
}
}
===============================
My Blog
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks