2014-10-21 3 views
0

내 배열의 미리 결정된 값은 판매 수치와 관련이 있습니다. 그러나 상점 값에 대한 사용자 입력을 받아들이 기 위해 어떻게 변경 될 수 있는지 궁금합니다. 나는 사용자 입력을 받아들이는 몇 가지 "for"루프를 보았지만 그들이 지그재그 배열이나 다차원 배열에서만 작동하는지 알지 못한다. 나는 그들 뒤에있는 논리를 이해하지도 않는다. 나는 그것이 단순한 개념이라는 것을 이해하지만 나는 아직 초보자입니다.사용자 배열 입력 C#

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); 
+0

'저장 [outerIterator] [innerIterator] = int.Parse (Console.ReadLine는());' – itsme86

+0

외부 배열 숙박 가정 (당신의 내면의 배열을 교체 fixed?)를'List'와 연결합니다. 이렇게하면 원하는만큼 여러 번'.Add()'할 수 있습니다. –

답변

2

사용자가 데이터를 입력 할 수 있도록 처리하려면 배열을 버리고 List<List<int>>을 사용하는 것이 가장 좋습니다. 왜? List<T>이 동적으로 커질 것이기 때문입니다. 그렇지 않으면 여전히 배열로 처리 할 수 ​​있지만 길이를 고정시켜야합니다 (또는 사용자가 먼저 제공해야합니다).

그래서 당신이 뭔가를 할 수 있습니다 :

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)); 
1

사용자 입력을 통해 stores을 확실히 기입 할 수 있습니다. 몇 가지 다른 방법이 있습니다. stores을 들쭉날쭉 한 배열로 유지하려면 배열의 크기를 먼저 찾아야합니다.

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()); 
    } 
} 

그 시점에서 끝났습니다. 귀하의 들쭉날쭉 한 배열이 가득 찼습니다.

List<int>int 어레이 대신에 더 쉽게 사용할 수 있습니다. List<int>을 사용하면 크기를 미리 결정하는 것에 대해 걱정할 필요가 없습니다. 간단하게 Add() 새 번호를 입력 할 수 있습니다.

+0

. 평균으로 평균을 구할 수 있습니까? –

+0

@JasonB 예. 가능합니다. 이전과 똑같은 방식으로하십시오. 한 문자를 변경할 필요가 없습니다. – itsme86