简体   繁体   中英

User array input C#

The predetermined values in my array pertain to sales numbers, however I was wondering how this could be changed to accept user input for value of the stores. I've seen a few "for" loops that accept user input but I do not know if they work for jagged arrays or only multidimensional; nor do I understand the logic behind them. I understand it is a simple concept but I'm still a beginner.

int[][] stores = {new int [] {2000,4000,3000,1500,4000},
                              new int [] {6000,7000,8000},
                              new int [] {9000,10000}};


        double av1 = stores[0].Average();
        double av2 = stores[1].Average();
        double av3 = stores[2].Average();
        double totalav = (av1 + av2 + av3) / 3;


        Console.WriteLine("Region 1 weekly sales average is: {0}", av1);
        Console.WriteLine("Region 2 weekly sales average is: {0}", av2);
        Console.WriteLine("Region 3 weekly sales average is: {0}", av3);
        Console.WriteLine("The total store average is: {0}", totalav);

The best to handle this to allow a user to enter data would probably be to ditch the arrays and use List<List<int>> instead. Why? Because List<T> will grow dynamically. Otherwise you can still do it with arrays, but you are going to have to have fixed lengths (or the user will have to supply those first).

So you could do something like this:

List<List<int>> stores = new List<List<int>>();
int storeNum = 0;

string input = "";
do 
{
    var store = new List<int>();
    Console.WriteLine(string.Format("Enter sales for store {0} (next to go to next store, stop to finish)",storeNum++ ));
    do   
    {
        int sales = 0;
        input = Console.ReadLine();
        if (int.TryParse(input, out sales)) 
        {
            store.Add(sales);
        }
        // Note: we are ignoring invalid entries here, you can include error trapping
        // as you want
    } while (input != "next")   // or whatever stopping command you want
    stores.Add(store);
} while (input != "stop")     // or any stopping command you want

// At this point you'll have a jagged List of Lists and you can use Average as before. For example:

var avg = stores[0].Average();

// Obviously, you can do this in a loop:

int total = 0;
for (int i=0; i<stores.Count; i++)   // note lists use Count rather than Length - you could also do foreach
{
    var avg = stores[i].Average();
    Console.WriteLine(string.Format("Store {0} average sales: {1}", i, avg);
    total += avg;
}
Console.WriteLine(string.Format("Overall Average: {0}", total / stores.Count));

You can definitely fill stores with user input. There are a few different ways. If you want to keep stores as a jagged array, you'll have to find out how large the array should be first.

Console.Write("Number of store groups: ");
int numberOfStoreGroups = int.Parse(Console.ReadLine());

int[][] stores = new int[numberOfStoreGroups][];

// Now loop through each group
for (int i = 0;i < numberOfStoreGroups;++i)
{
    Console.Write("Number of stores in group {0}: ", i + 1);
    int groupSize = int.Parse(Console.ReadLine());
    stores[i] = new int[groupSize];

    // Now loop through each store in the group
    for (int j = 0;j < groupSize;++j)
    {
        Console.Write("Store number {0}: ", j + 1);
        stores[i][j] = int.Parse(Console.ReadLine());
    }
}

At that point you're done. Your jagged array is filled.

You might find it easier to use a List<int> instead of an int array though. With a List<int> , you don't need to worry about predetermining the size. You can simply Add() a new number to it.

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