Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Better switch statement.

Better switch statement.

Scheduled Pinned Locked Moved Clever Code
visual-studiocombusinesscareer
4 Posts 4 Posters 48 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Amy
    wrote on last edited by
    #1

    I got something like this in the morning while taking an interview:

    using System;
    class SwitchTest
    {
    public static void Main()
    {
    Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
    Console.Write("Please enter your selection: ");
    string s = Console.ReadLine();
    int n = int.Parse(s);
    int cost = 0;
    switch(n)
    {
    case 1:
    cost += 25;
    break;
    case 2:
    cost += 25;
    goto case 1;
    case 3:
    cost += 50;
    goto case 1;
    default:
    Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
    break;
    }
    if (cost != 0)
    Console.WriteLine("Please insert {0} cents.", cost);
    Console.WriteLine("Thank you for your business.");
    }
    }

    :) Finally I am able to find: From Here[^] it got copied.

    Warm Regards. --Amit

    P V B 3 Replies Last reply
    0
    • _ _Amy

      I got something like this in the morning while taking an interview:

      using System;
      class SwitchTest
      {
      public static void Main()
      {
      Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
      Console.Write("Please enter your selection: ");
      string s = Console.ReadLine();
      int n = int.Parse(s);
      int cost = 0;
      switch(n)
      {
      case 1:
      cost += 25;
      break;
      case 2:
      cost += 25;
      goto case 1;
      case 3:
      cost += 50;
      goto case 1;
      default:
      Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
      break;
      }
      if (cost != 0)
      Console.WriteLine("Please insert {0} cents.", cost);
      Console.WriteLine("Thank you for your business.");
      }
      }

      :) Finally I am able to find: From Here[^] it got copied.

      Warm Regards. --Amit

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Wrong forum. Try Hall of Shame.

      1 Reply Last reply
      0
      • _ _Amy

        I got something like this in the morning while taking an interview:

        using System;
        class SwitchTest
        {
        public static void Main()
        {
        Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
        Console.Write("Please enter your selection: ");
        string s = Console.ReadLine();
        int n = int.Parse(s);
        int cost = 0;
        switch(n)
        {
        case 1:
        cost += 25;
        break;
        case 2:
        cost += 25;
        goto case 1;
        case 3:
        cost += 50;
        goto case 1;
        default:
        Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
        break;
        }
        if (cost != 0)
        Console.WriteLine("Please insert {0} cents.", cost);
        Console.WriteLine("Thank you for your business.");
        }
        }

        :) Finally I am able to find: From Here[^] it got copied.

        Warm Regards. --Amit

        V Offline
        V Offline
        VallarasuS
        wrote on last edited by
        #3

        This deserves 5 in hall of shame!! :thumbsup:

        Regards Vallarasu S | FSharpMe.blogspot.com

        1 Reply Last reply
        0
        • _ _Amy

          I got something like this in the morning while taking an interview:

          using System;
          class SwitchTest
          {
          public static void Main()
          {
          Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
          Console.Write("Please enter your selection: ");
          string s = Console.ReadLine();
          int n = int.Parse(s);
          int cost = 0;
          switch(n)
          {
          case 1:
          cost += 25;
          break;
          case 2:
          cost += 25;
          goto case 1;
          case 3:
          cost += 50;
          goto case 1;
          default:
          Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
          break;
          }
          if (cost != 0)
          Console.WriteLine("Please insert {0} cents.", cost);
          Console.WriteLine("Thank you for your business.");
          }
          }

          :) Finally I am able to find: From Here[^] it got copied.

          Warm Regards. --Amit

          B Offline
          B Offline
          bVagadishnu
          wrote on last edited by
          #4

          This is clever code. Just think what would happen if a Starbucks customer typed in Venti at the prompt. :)

          Schenectady? What am I doing in Schenectady?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups