Write the following code in 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.
时间: 2023-07-03 16:10:24 浏览: 101
ISO/IEC 14882:2014 C++14 C++标准 英文版 PDF 非扫描版
5星 · 资源好评率100%
Sure, here's the code in C#:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
char choice;
do
{
DisplayMenu();
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
switch (choice)
{
case 'B':
// Add code to handle Calculate Number functionality
break;
case 'E':
Console.WriteLine("Exiting...");
break;
default:
Console.WriteLine("Invalid choice. Please try again.");
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: ");
}
}
```
Explanation:
- We start by declaring a `char` variable called `choice` to store the user's menu choice.
- In the `Main` method, we use a `do-while` loop to repeatedly display the menu and prompt the user for a choice, until they enter 'E' to exit.
- Inside the loop, we use a `switch` statement to check the user's choice and perform the appropriate action. If they choose 'B', we will add code to handle the Calculate Number functionality. If they choose 'E', we simply print a message and the loop will exit.
- The `DisplayMenu` method simply prints the menu to the console.
阅读全文