Ak Java Freak
Posts : 23 Join date : 2009-02-07
| Subject: 1st year students read and study this GoTo command Sat Feb 09, 2013 1:57 am | |
| - Code:
-
using System; using System.Collections; using System.Linq; using System.Text;
namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string myInput; int myInt;
begin: // <----- notice the begin here once we type continue this program will jump back here and read the below code again
Console.Write("Please enter a number between 1 and 3: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput);
// Switch with integer type switch (myInt) { case 1: Console.WriteLine("Your number is {0}.",myInt); break; case 2: Console.WriteLine("Your number is {0}.",myInt); break; case 3: Console.WriteLine("Your number is {0}.",myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.",myInt); break; }
decide: // <--- if your input was incorrect this will be triggered
Console.Write("Type\"continue\"to go on or\"quit\"to stop: "); myInput = Console.ReadLine();
// switch with string type switch (myInput) { case "continue": goto begin; // <---- goto command is use to jump from statements to another statement but this is not a good programming practice case "quit": Console.WriteLine("bye."); break; defualt: Console.WriteLine("Your input {0} is incorrect.",myInput); goto decide; // <-- same here i use goto to make this program student friendly } } } }
| |
|