Hi I wrote a code that supposed display the name of each person and then able to select which ever person I pick, then it display they grade and let me able to change it. I got it to display they grade but I don't know how to change that persons grade value only, instead it changes every persons grade and displays it how do I change it? So it displays the change of that one person only? and add whatever value I want to it? And how do I display the changed value next to the persons name? If you could help me I be very thank full.
static void Main(string[] args)
{
int selection;
do
{
String[] A = { "Tim", "Jhon", "Sam", "Derp"};
int[] B = { 90, 89, 60, 40 };
DisplayMenu();
Console.Write("Enter you selection: ");
int.TryParse(Console.ReadLine(),out selection);
switch (selection)
{
case 1:
Console.WriteLine(
"{0} ", B[0]);
AddToGrade(B);
break;
case 2:
Console.WriteLine(
"{0} ", B[1]);
AddToGrade(B);
break;
case 3:
Console.WriteLine(
"{0} ", B[2]);
AddToGrade(B);
break;
default:
Console.WriteLine(
"{0} ", B[3]);
AddToGrade(B);
break;
}
Console.ReadKey();
} while (selection != 4);
}
static void DisplayMenu()
{
Console.Clear();
Console.WriteLine("Select a student 1-4");
Console.WriteLine("1. Time");
Console.WriteLine("2. Jhon");
Console.WriteLine("3. Sam");
Console.WriteLine("4. Derp");
}
static void AddToGrade(int[] array)
{
Console.WriteLine("Input New Value: ");
int newValue = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < array.Length; i++)
{
array[i] += newValue;
Console.Write("New Grade: {0}", array[i]);
}
}
AddToGrade
should accept index
representing the grade to change.
static void AddToGrade(int[] array ,int index)
{
Console.WriteLine("Input New Value: ");
int newValue = Convert.ToInt32(Console.ReadLine());
array[index] += newValue;
Console.Write("New Grade: {0}", array[index]);
}
And after your switch (before the Console.ReadKey();
) just call this:
AddToGrade(B, selection - 1);
Moreover, you should use more functions to make your code robust and elegant, eg (That example will also alliw you to keep the formatting in a single place)
static void DisplayNameGrade(int index, String[] A, int[] B)
{
Console.WriteLine("Name: {0}, Grade: {1}", A[index], B[index])
}
Generally, you should learn the basic concepts of Object Oriented Programming , and design your code with classes, members and functions.
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.