Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(String[]args)
        {
            int n;
            Console.WriteLine("enter your choice ");
            n =   Console.ReadLine();
            switch (n)
            {
                case 1: if (n == 1)
                        Console.WriteLine("1:Monday");
                    break;


                case 2: if (n == 2)
                        Console.WriteLine("2:Tuesday");
                    break;

                case 3: if (n == 3)
                        Console.WriteLine("3:Wednesday");
                    break;

                case 4: if (n == 4)
                        Console.WriteLine("4:Thursday");
                    break;


                case 5: if (n == 5)
                        Console.WriteLine("5:friday");
                    break;


                case 6: if (n == 6)
                        Console.WriteLine("6:saturday");
                    break;

                case 7: if (n == 7)
                        Console.WriteLine("7:sunday");
                    break;

                default: Console.WriteLine("wrong choice");
                    break;
            }
        }
    }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 6-Mar-14 4:57am
v3

You don't need the if blocks inside the case ones.
So we could reduce your code to:
C#
static void Main(String[]args)
{
   string input;
   Console.WriteLine("enter your choice ");
   input = Console.ReadLine();
   int n;
   if (int.TryParse(input, out n)) {
      switch (n) {
         case 1:
            Console.WriteLine("1:Monday");
            break;

         case 2:
            Console.WriteLine("2:Tuesday");
            break;

         case 3:
            Console.WriteLine("3:Wednesday");
            break;

         case 4:
            Console.WriteLine("4:Thursday");
            break;

         case 5:
            Console.WriteLine("5:friday");
            break;

         case 6:
            Console.WriteLine("6:saturday");
            break;

         case 7:
            Console.WriteLine("7:sunday");
            break;

         default:
            Console.WriteLine("wrong choice");
            break;
      }
   }
   else {
      Console.WriteLine("Wrong input! You did not seem to enter a valid integer.");
   }
}


Hope this helps.
 
Share this answer
 
v2
ReadLine returns a string, so you can't assign it to an integer value. You need to look at it first, make sure it is a number and then convert it to one.
Try this:
C#
int n;
Console.WriteLine("enter your choice ");
string s = Console.ReadLine();
if (!int.TryParse(s, out n))
    {
    Console.WriteLine("\"{0}\" is not a number", s);
    }
else
    {
    switch (n)
        {
        case 1: if (n == 1)
                Console.WriteLine("1:Monday");
            break;


        case 2: if (n == 2)
                Console.WriteLine("2:Tuesday");
            break;

        case 3: if (n == 3)
                Console.WriteLine("3:Wednesday");
            break;

        case 4: if (n == 4)
                Console.WriteLine("4:Thursday");
            break;


        case 5: if (n == 5)
                Console.WriteLine("5:friday");
            break;


        case 6: if (n == 6)
                Console.WriteLine("6:saturday");
            break;

        case 7: if (n == 7)
                Console.WriteLine("7:sunday");
            break;

        default: Console.WriteLine("wrong choice");
            break;
        }
    }
 
Share this answer
 
Comments
phil.o 6-Mar-14 11:56am    
switch n {
case 1: if (n == 1) {
// ...
}
}

Oh my God! :)
OriginalGriff 6-Mar-14 12:30pm    
I know, I know...:sigh:
phil.o 6-Mar-14 15:35pm    
That was weird and wonderful ^^
george4986 6-Mar-14 14:01pm    
nice comment ;-)
phil.o 6-Mar-14 15:35pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900