How to Make User Input into Array and Pass It to a Method in C#?

  • A+

So I have this very simple program that I have created that gets the user input and then pass the values to the method named average. Everything is perfectly fine, however, something crossed my mind like "What if the user only enters two numbers or an array of numbers?". I know that I have to use array as a parameter, but somehow I am not sure how to accomplish what i want to do. Do you guys have any tips on how I can achieve what i am trying to do? My code is here:

using System;  namespace Averages { class Program {     static void Main(string[] args)     {                             Console.WriteLine("Enter number(s): ");         double num1 = Convert.ToInt32(Console.ReadLine());         double num2 = Convert.ToInt32(Console.ReadLine());         double num3 = Convert.ToInt32(Console.ReadLine());         average(num1, num2, num3);                  Console.ReadKey();                }     static void average(double num1, double num2, double num3)     {         double avg = (num1 + num2 + num3) / 3;         Console.WriteLine("You have entered: " + num1 + ", " + num2 + ", " + num3);         Console.WriteLine("The average is: " + avg);                                      }  } } 

I would appreciate any help. Thank you!


you can ask user for input of comma delimited numbers. Or, you can repeat input until user enters something like 0. I that case, you can change your program into this:

using System; using System.Collections.Generic; using System.Linq;  namespace Averages {     class Program     {         static void Main(string[] args)         {             //list to hold your numbers, way more flexible than array             List<double> enteredNubers = new List<double>();              //message user             Console.WriteLine("Enter number(s) or 0 to end: ");              //run this indefinitely             while (true)             {                 //take user input                 string userinput = Console.ReadLine().Trim();                 //if user enters 0, exit this loop                 if (userinput == "0")                     break;                   double num;                 //try to convert text ot number                 if (double.TryParse(userinput, out num))                 {                     //if it is successfull, add number to list                     enteredNubers.Add(num);                 }                 else //else message user with errorr                     Console.WriteLine("Wrong input. Please enter number or 0 to end");             }              //when loop is exited (wen user entered 0), call method that calculates average             Average(enteredNubers);             Console.ReadKey();         }         static void Average(List<double> numbers)         {             double sum = 0;             //go through list and add each number to sum             foreach (double num in numbers)             {                 sum += num;             }             //or, you can sum it using linq like this:             //sum = numbers.Sum();              //show message  - all the entered numbers, separated by comma             Console.WriteLine("You have entered: " + string.Join(", ", numbers.ToArray()));             //write average             Console.WriteLine("The average is: " + sum/numbers.Count);         }     } } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: