nisana begam

nisana begam

  • NA
  • 3
  • 782

how to accept char & integer from user in switch in c#

Mar 15 2016 4:43 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
// PrintMenu();
Console.WriteLine("L - LED");
Console.WriteLine("T - Get RTC Time");
Console.WriteLine( "S - Set RTC time");
Console.WriteLine( "2 - UART 2 test");
Console.WriteLine( "3 - UART 3 Test");
Console.WriteLine( "4 - UART 4 Test");
Console.WriteLine( "5 - UART 5 Test");
Console.WriteLine( "d - Digital I/O Test");
Console.WriteLine( "a - Analog I/O Test");
Console.WriteLine( "X. Exit");
var input = Console.ReadKey();
var key = input.KeyChar;
int value;
if (int.TryParse(key.ToString(), out value)) {
//just to move to a new line
Console.WriteLine();
RouteChoice(value);

}
else {
Console.WriteLine("Invalid Entry.");
}
Console.Write("Press any key to exit...");
Console.ReadKey(false);
}
// private static void PrintMenu() {
// throw new NotImplementedException();
// }
private static void RouteChoice(int menuChoice) {

Console.WriteLine("Menu choice");
switch(menuChoice) {
case 'l':
Console.WriteLine(" LED");
break;
case 't':
Console.WriteLine(" Get RTC Time");
break;
case 's':
Console.WriteLine("Set RTC time");
break;
case 2:
Console.WriteLine(" UART 2 test");
break;
case 3:
Console.WriteLine(" UART 3 test");
break;
case 4:
Console.WriteLine(" UART 4 test");
break;
case 5:
Console.WriteLine(" UART 5 test");
break;
case 'd':
Console.WriteLine(" Digital i/o test");
break;
case 'a':
Console.WriteLine("Analog i/o test");
break;
case 'x':
Console.WriteLine("Exit");
return;
}
}
}


Answers (2)