2014-04-04 65 views
0

因此,我必須要求用戶輸入一組5到15個數字,直到他們進入EOP。如何將這些數字保存在數組中?使用數組中的數字,我將不得不做一些其他的東西,比如列出它們,找到平均值等。但是我不知道如何將用戶輸入的數字保存到數組中。如何將來自用戶的輸入存儲在數組中?

Console.WriteLine("Please enter a set of grades. Min 5 grades, Max 15 grades:"); 
Console.WriteLine("To show the menu, enter -99"); 

for (int y = 0; y < 16; y++) 
{ 
    Console.WriteLine("Enter grade:"); 
    strGrades = Console.ReadLine(); 
    intGrades = Int32.Parse(strGrades); 

    if (intGrades == -99) 
    { 
    System.Console.WriteLine("1. Number of values in the array\n"); 
    System.Console.WriteLine("2. List the values in the array\n"); 
    System.Console.WriteLine("3. Average\n"); 
    System.Console.WriteLine("4. Delete a specific value \n"); 
    System.Console.WriteLine("5. Clear all the values in the array\n"); 
    System.Console.WriteLine("6. Change a specific value\n"); 
    System.Console.WriteLine("7. Exit"); 

    strChoice = Console.ReadLine(); 
    Choice = Int32.Parse(strChoice); 

    int[] arr = new int[15]; 

    for (int x = 0; x <= arr.Length; x++) 
    { 
     arr[x] = intGrades; 
     arr[x] = Int32.Parse(Console.ReadLine()); 
     intCounter++; 

     if (intGrades == -99) 
     { 
     intCounter--; 
     } 
    } 
+0

這段代碼甚至沒有編譯。如果你正在尋找幫助,你應該發佈代碼。 – computerfreaker

+2

這是功課嗎? – Enigmativity

回答

0

嘗試使用列表

Console.WriteLine("Please enter a set of grades. Min 5 grades, Max 15 grades:"); 
Console.WriteLine("To show the menu, enter -99"); 

List<int> lstGrades=new List<int>(); 

for (int y = 0; y < 16; y++) 
{ 
    Console.WriteLine("Enter grade:"); 
    strGrades = Console.ReadLine(); 
    intGrades = Int32.Parse(strGrades); 

    lstGrades.Add(intGrades); 

    if (intGrades == -99) 
    { 
    System.Console.WriteLine("1. Number of values in the array\n"); 
    System.Console.WriteLine("2. List the values in the array\n"); 
    System.Console.WriteLine("3. Average\n"); 
    System.Console.WriteLine("4. Delete a specific value \n"); 
    System.Console.WriteLine("5. Clear all the values in the array\n"); 
    System.Console.WriteLine("6. Change a specific value\n"); 
    System.Console.WriteLine("7. Exit"); 

    strChoice = Console.ReadLine(); 
    Choice = Int32.Parse(strChoice); 

    /*int[] arr = new int[15]; 

    for (int x = 0; x <= arr.Length; x++) 
    { 
     arr[x] = intGrades; 
     arr[x] = Int32.Parse(Console.ReadLine()); 
     intCounter++; 
     if (intGrades == -99) 
     { 
     intCounter--;*/ 
     } 
    } 
0

我會做這樣的事情:

請記住,對於清單,你需要使用System.Collections.Generic

using System.Collections.Generic; 

所以...程序:

static void Main() 
     { 
      Console.WriteLine("To show the menu, enter -99"); 
      Console.WriteLine("Please enter a set of grades. Min 5 grades, Max 15 grades: "); 
      List<int> gradesList =new List<int>(); 
      bool exit=false; 
      do 
      { 
       int x = 0; 
       int.TryParse(Console.ReadLine(), out x); 
       switch(x) 
       { 
        case -99: 
         exit = true; 
         break; 
        case 0: 
         Console.WriteLine("Please insert integer values:"); 
         break; 
        default: 
         gradesList.Add(x); 
         break; 
       } 
      } while(!exit); 
      ShowMenu(); 

     } 
     static void ShowMenu() 
     { 
      bool exit=false; 
      do 
      { 
       Console.WriteLine("1. Number of values in the array"); 
       Console.WriteLine("2. List the values in the array"); 
       Console.WriteLine("3. Average"); 
       Console.WriteLine("4. Delete a specific value"); 
       Console.WriteLine("5. Clear all the values in the array"); 
       Console.WriteLine("6. Change a specific value"); 
       Console.WriteLine("7. Exit"); 

       int x = 0; 
       int.TryParse(Console.ReadLine(), out x); 

       switch(x) 
       { 
        case 1: 
         //number of values in the array 
         break; 
        case 2: 
         //list of values in the array 
         break; 
        case 3: 
         //average 
         break; 
        case 4: 
         //delete a specific value 
         break; 
        case 5: 
         //clear all values in the array 
         break; 
        case 6: 
         //change a specific value 
         break; 
        case 7: 
         exit = true; 
         break; 
        default: 
         Console.WriteLine("Invalid option"); 
         break; 
       } 
      } while(!exit); 
     } 
相關問題