Friday 4 July 2014

C# Conditional statements switch case Write a C# program to detect key presses.

If the user pressed number keys( from 0 to 9), the program will display the number that is pressed, otherwise the program will show "Not allowed". 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
char key;
Console.Write("Press a number key:");
key = (char)Console.Read();
switch (key)
{
case '0': Console.WriteLine("You pressed 0"); break;
case '1': Console.WriteLine("You pressed 1"); break;
case '2': Console.WriteLine("You pressed 2"); break;
case '3': Console.WriteLine("You pressed 3"); break;
case '4': Console.WriteLine("You pressed 4"); break;
case '5': Console.WriteLine("You pressed 5"); break;
case '6': Console.WriteLine("You pressed 6"); break;
case '7': Console.WriteLine("You pressed 7"); break;
case '8': Console.WriteLine("You pressed 8"); break;
case '9': Console.WriteLine("You pressed 9"); break;
default: Console.WriteLine("Not allowed!"); break;

            }


     }
  }
}

No comments:

Post a Comment