2015-11-02 36 views
1

所以我有這個代碼。我擁有它,當用戶輸入一個數字1-4時,它會根據輸入的數字在開頭顯示不同的說法和名稱。我想要做的就是讓它在輸入-1而不是1,2,3或4時允許他們輸入他們自己的說法來輸出。我怎麼能在一個switch語句,讓用戶輸入一個字符串顯示

using System; 

namespace TryIt 
{  
class Conversation 
    { 
    // Declare constants that define the input range 
    private const int MIN = -1; 
    private const int MAX = 4; 

    // Declare constants for the colors to be used 
    private const ConsoleColor INPUT_COLOR = ConsoleColor.Green; 
    private const ConsoleColor PROMPT_COLOR = ConsoleColor.Blue; 
    private const ConsoleColor ERROR_COLOR = ConsoleColor.Red; 
    private const ConsoleColor MESSAGE_COLOR = ConsoleColor.White; 
    private const ConsoleColor BACKGROUND_COLOR = ConsoleColor.DarkGray; 

    private String name; 

    public Conversation() 
    { 
     Console.Title = "The Best TryIt Program"; 
     Console.BackgroundColor = BACKGROUND_COLOR; 
     Console.Clear(); 
    } 

    public void go() 
    { 
     getName(); 
     int number = getNumber(); 
     displayMessage(number); 

     Console.ForegroundColor = ConsoleColor.Gray; 
     Console.WriteLine(); 
    } 

    private void getName() 
    { 
     Console.ForegroundColor = PROMPT_COLOR; 
     Console.Write("Enter your name. "); 
     Console.ForegroundColor = INPUT_COLOR; 

     // This will ask what your name is and prompt you to enter it. 
     name = Console.ReadLine(); 
    } 

    private int getNumber() 
    { 
     int input; 

     // int input will take your name and display it back to you 
     do 
     { 
      Console.ForegroundColor = PROMPT_COLOR; 
      Console.Write("\nEnter a number from 1 to 4. "); 
      Console.ForegroundColor = INPUT_COLOR; 

      // This will ask you for a number 
      input = Convert.ToInt16(System.Console.ReadLine()); 

      // If you don't put in a valid number it will tell you 
      if (input < MIN || input > MAX) 
      { 
       Console.ForegroundColor = ERROR_COLOR; 
       Console.WriteLine("Invalid Number!"); 
      } 
     } while (input < MIN || input > MAX); 

     return input; 
    } 

    private void displayMessage(int choice) 
    { 
     String phrase = " "; 

     // Declares the variable (a string) for the output messages. 
     switch (choice) 
     { 
      case 1: phrase = " is a genius!"; break; 
      case 2: phrase = " is amazing!"; break; 
      case 3: phrase = " came to chew bubblegum" + 
        ", kick butt " + "and is all out of gum."; break; 
      case 4: phrase = " is a rockstar!"; break; 
     } 


     Console.WriteLine(); 
     Console.ForegroundColor = MESSAGE_COLOR; 

     // This will display the message you selected 10 times 
     for (int counter = 0; counter < 10; counter++) 
     { 
      Console.WriteLine(counter + ") " + name + phrase); 
     } 
    } 
} 

}

回答

4
switch(choice) 
{ 
    case 1: ... 
     . 
     . 
     . 
    case -1: Console.WriteLine("Put in your own saying: "); 
      phrase = " " + Console.ReadLine(); 
      break; 
} 

此開關的情況下會問你打字時「-1」就擺在俗話說,然後將其寫入到短語。在開關盒之後,您可以像上面的代碼那樣進行操作。

相關問題