Below is my code
class Program
{
static void Main(string[] args)
{
int[] input = new int[5];
Console.WriteLine("Pls input 5 nums:\n");
for (int i = 0; i < 5; i++)
{
Console.Write("Number [" + i + "] is ");
input[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Nums u just entered are: ");
for (int i = 0; i < 5; i++)
{
Console.Write(input[i] + " ");
Console.ReadLine();
}
}
}
}
I expect the outcome lists all the numbers horizontally as a array instead of entering many times. Please advise how to do that.
Thanks,
Ice
Loading
Kirtan PatelPosted Oct 26, 2009, 9:43 AM
Here is your Corrected Code to Show Number Horizontally
dont forget to mark "Do you like this Answer" please :)
static void Main(string[] args)
{
int[] input = new int[5];
Console.WriteLine("Pls input 5 nums:\n");
for (int i = 0; i < 5; i++)
{
Console.Write("Number [" + i + "] is ");
input[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Nums u just entered are: ");
for (int i = 0; i < 5; i++)
{
Console.Write(input[i] +"\t");
}
Console.ReadKey();
}
KhoiPosted Oct 26, 2009, 11:39 AM
Thanks a lot
Ice.
Girish KulkarniPosted Oct 26, 2009, 9:45 AM
You can accept the inputs in a single line. You need to decide what seperator you are going to use for the values eg. 1,2,3,4,5 (here comma is seperator).
Now split the input string by using seperator and store it into your array.
Your coode will something looks like following.
class Program
{
static void Main(string[] args)
{
int[] input = new int[5];
Console.WriteLine("Please enter comma seperated string : ");
string myString = Console.ReadLine();
string[] StringInputs = myString.Split(',');
//I am assuming that user will enter 5 inputs
int counter=0;
foreach (string s in StringInputs)
{
if (s.Trim().ToString() != "")
{
input[counter] = Convert.ToInt32(s.ToString());
counter += 1;
}
}
Console.WriteLine("output:");
foreach (int myValue in input)
{
Console.WriteLine(myValue);
}
Console.ReadLine();
}
}
Please mark this post as Answered if this solution really worked for you.
Thanks and Enjoy coding. It's my first post in forum and hope it will help you.