2015-11-22 4 views
0
static void Main(string[] args) 
    { 

     Console.WriteLine("************************************************"); 
     Console.WriteLine("*    CAESAR CIPHER     *"); 
     Console.WriteLine("*    -------------     *"); 
     Console.WriteLine("* 1) Encrypt plain text      *"); 
     Console.WriteLine("* 2) Decrypt plain text      *"); 
     Console.WriteLine("* 3) Quit         *"); 
     Console.WriteLine("************************************************"); 

     Console.Write("Enter your option : "); 
     string num = Console.ReadLine(); 
     int cnum = Convert.ToInt32(num); 

     Console.WriteLine(); 
     string n = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     string m = "DEFGHIJKLMNOPQRSTUVWXYZABC"; 

     if (cnum==1) 
     { 
     Console.Write("Enter a string : "); 
     string text = Console.ReadLine(); 

     Console.WriteLine(); 
     Console.WriteLine("Applying Caesar cipher ..."); 
     Console.WriteLine(); 
     text = text.Replace(" ", ""); 
     text = text.ToUpper(); 
     Console.WriteLine("Input Text = " + text); 
     text = text.ToUpper(); 
     Console.Write("Output Text = "); 
     text = text.Replace(" ", ""); 
     foreach (char i in text) 
     { 
      if (i >= 'A' && i <= 'Z') 
      { 
       int pos = n.IndexOf(i); 
       Console.Write(m[pos]); 

      } 
      else 
      { 
       Console.Write(i); 

      } 

     } 
      Console.WriteLine(); 
      Console.WriteLine("Press any key to return to menu"); 

     } 
     else if (cnum == 2) 
     { 
      Console.Write("Enter a string : "); 
      string text = Console.ReadLine(); 

      Console.WriteLine(); 
      Console.WriteLine("Applying Caesar cipher ..."); 
      Console.WriteLine(); 
      text = text.Replace(" ", ""); 
      text = text.ToUpper(); 
      Console.WriteLine("Input Text = " + text); 
      text = text.ToUpper(); 
      Console.Write("Output Text = "); 
      text = text.Replace(" ", ""); 
      foreach (char i in text) 
      { 
       if (i >= 'A' && i <= 'Z') 
       { 
        int pos = m.IndexOf(i); 
        Console.Write(n[pos]); 
       } 
       else 
       { 
        Console.Write(i); 

       } 

      } 
      Console.WriteLine(); 
      Console.WriteLine("Press any key to return to menu"); 
     } 

     else 
     { 
      Console.WriteLine("Bye!"); 
     } 
      Console.ReadKey(); 
    } 
} 
} 

Это мой код. Как мне вернуться в мое меню? Я попробовал утверждение goto, но он сразу же вернулся к моему меню. Я хочу нажать любую клавишу, чтобы вернуться к моему заявлению меню. Я не уверен, как это сделать, так может помочь мне здесь? Могу ли я использовать оператор switch и все еще делать это, используя инструкции else?Как мне вернуться в мое меню?

ответ

1

Просто заверните весь свой код в блок while (true), чтобы повторять его снова и снова. Чтобы выскочить из него, используйте break, когда пользователь вводит 3:

static void Main(string[] args) 
{ 
    while (true) 
    { 
     // your existing code 

     if (cnum == 1) 
     { 
      // your existing code 
     } 
     else if (cnum == 2) 
     { 
      // your existing code 
     } 
     else 
     { 
      Console.WriteLine("Bye!"); 
      break; 
     } 
     Console.ReadKey(); 
    } 
}