Write the following code in c #,2. Modify your main so that the above method is called 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.
时间: 2024-01-31 18:02:13 浏览: 89
cin.rar_The Program
Sure, here's the modified code that calls the `DisplayMenu` method repeatedly until the user enters "E":
```csharp
using System;
public class Program
{
static void Main()
{
string userChoice;
do
{
DisplayMenu();
userChoice = Console.ReadLine().ToUpper();
switch (userChoice)
{
case "A":
Console.WriteLine("Calculate Distance not implemented yet.");
break;
case "E":
Console.WriteLine("Exiting program...");
break;
default:
Console.WriteLine("Invalid choice. Please enter A or E.");
break;
}
Console.WriteLine();
} while (userChoice != "E");
}
static void DisplayMenu()
{
Console.WriteLine("Calculation Menu, developed by StudentID:xxxx");
Console.WriteLine("A) Calculate Distance");
Console.WriteLine("E) To Exit");
Console.Write("Enter the Letter that corresponds to your choice: ");
}
}
```
Note that this code uses a `do-while` loop to repeatedly call the `DisplayMenu` method and handle user input until the user enters "E". The `ToUpper` method is used to convert the user's input to uppercase so that we can handle lowercase and uppercase letters in the same way. The `switch` statement is used to perform the corresponding action for each menu choice, or display an error message for invalid choices.
阅读全文