andres casado
andres casado

Reputation: 57

how can you get multiple inputs in c# on one line for example ( 1 2 3 4 5) without having to enter one by one

can input one by one by pressing enter but im trying to have it read one on line separated by spaces.

 void readTestScores()
    {
        int i;
        double tests;

        Console.Write ("ENTER EXAM SCORE\t: ");
        exam = Convert.ToInt32(Console.ReadLine());
        Console.Write ("ENTER TEST SCORES\t: ");
        for (i = 0; i < 7; i++) 
        {
            tests = Convert.ToDouble (Console.ReadLine ());
            testavg += tests;
        }
        testavg /= 7;
        Console.WriteLine ("TEST AVERAGE IS\t\t: {0}",testavg);
    }

Upvotes: 1

Views: 3307

Answers (1)

sowrd299
sowrd299

Reputation: 149

Look into the string split method, as documented here:

https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx

Usage would be to the effect of:

string s = Console.ReadLine();
double[] input = s.Split(' ').Select(t => Convert.ToDouble(t)).ToArray();

Upvotes: 3

Related Questions