因此,在进入EOP之前,我必须要求用户提供一组5到15个数字。如何将这些数字保存在数组中?使用数组中的那些数字,我将不得不做一些其他的事情,比如列出它们,找到平均数等等。但是我无法弄清楚如何将用户输入的数字保存到数组中。
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 :(得分:0)
尝试使用List
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--;*/
}
}
答案 1 :(得分:0)
我会做这样的事情:
请记住,对于List,您需要使用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 );
}