Write the following code using c #,1. Write a method with the following specifications: name: DisplayMenu arguments: none return value: none tasks: display the following menu choice on the screen Calculation Menu, developed by StudentID:xxxx B) Calculate Number E) To Exit Enter the Letter that corresponds to your choice: Please update your ID in the menu. You don’t have to implement the functionalities of the various menu choices at this stage Call this method from your main. 2. Modify your main so that the above method is call repeatedly. The program will terminate when the user enters E. Any other choice should produce an error message. Because you will not be doing any arithmetic you may accept the user response either as an int, or a char or a string. 3. Write a method with the following specifications: name: CalculateN arguments: an int array with values ranging from 0 to 10 return value: int array of 3 integers. The first element will indicate the total sum of all the values in the array, the second element will indicate the average of all the values in the array, the third element will indicate the smallest value in the array. displays: none tasks: The method will create an int array of 3 elements (call this the result). In your main you will generate an int array of 5 elements with values ranging from 0 to 10, call this method and display the resulting array’s value.
时间: 2024-02-13 10:02:16 浏览: 191
Here's the code in C#:
```
using System;
class Program
{
static void Main(string[] args)
{
char choice;
do
{
DisplayMenu();
choice = Console.ReadLine().ToUpper()[0];
switch (choice)
{
case 'B':
int[] values = new int[] { 2, 5, 7, 3, 1 };
int[] result = CalculateN(values);
Console.WriteLine("Total sum: " + result[0]);
Console.WriteLine("Average: " + result[1]);
Console.WriteLine("Smallest value: " + result[2]);
break;
case 'E':
Console.WriteLine("Exiting program...");
break;
default:
Console.WriteLine("Invalid choice!");
break;
}
Console.WriteLine();
} while (choice != 'E');
}
static void DisplayMenu()
{
Console.WriteLine("Calculation Menu, developed by StudentID:xxxx");
Console.WriteLine("B) Calculate Number");
Console.WriteLine("E) To Exit");
Console.Write("Enter the Letter that corresponds to your choice: ");
}
static int[] CalculateN(int[] values)
{
int sum = 0;
int min = int.MaxValue;
foreach (int value in values)
{
sum += value;
if (value < min)
{
min = value;
}
}
int average = sum / values.Length;
return new int[] { sum, average, min };
}
}
```
Note: Replace "xxxx" in the DisplayMenu method with your actual student ID.
阅读全文