I was given an assignment for class in which they want us to make an array of 10 and have a menu with several prompts such as changing numbers of the array, getting the sum of the array, etc. They want each menu option to have its own method with variations of:
public void enterNum(int[] args) I am having issues understanding how to store an array between the 5 methods when they are required to be set to void as well as I am not sure how to format when sending the info to the method. I have built a draft as a switch statement
using System; namespace Lab { class Program { static void Main(string[] args) { int input, x, y, sum = 0; int[] initArray = new int[10]; do { Console.Write("Would you like to: \n1) Enter a number\n2)Print the array \n3)find the sum of the array\n4)Reset the array\n5)Quit\n"); input = Convert.ToInt32(Console.ReadLine()); switch (input) { case 1: Console.Write("Enter the slot: "); x = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the new value: "); y = Convert.ToInt32(Console.ReadLine()); initArray[x] = y; break; case 2: for (int a = 0; a < 10; a++) { Console.Write(" | " + initArray[a]); } Console.Write("\n"); break; case 3: for (int b = 0; b < 10; b++) { sum += initArray[b]; } sum /= 10; Console.WriteLine(sum); sum = 0; break; case 4: for (int c = 0; c < 10; c++) { initArray[c] = 0; } break; } } while (input != 5); } } } an example of where I'm having issues is with
public static void Main(string[] args) { int[] NewArr = new int[10]; enterNum(NewArr); //trying to change number of array printSum(NewArr); //getting sum from array established in Main } public void enterNum(int[] args) { Console.Write("Enter the slot: "); int x = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the new value: "); int y = Convert.ToInt32(Console.ReadLine()); NewArr[x] = y; return; } public void printSum(int[] args) { for (int b = 0; b < 10; b++) { sum += NewArr[b]; } sum /= 10; Console.WriteLine(sum); return; } https://stackoverflow.com/questions/66683216/using-void-return-type-and-take-in-an-array-of-integers-as-a-parameter March 18, 2021 at 08:35AM
没有评论:
发表评论