ユーザーが表示する文字列を入力できるようにするswitchステートメントにケースを含めるにはどうすればよいですか?

user5249863

だから私はこのコードを持っています。ユーザーが1〜4の数字を入力すると、入力した数字に基づいて、最初に別のことわざと名前が表示されるようにしています。私がやりたいのは、1、2、3、4ではなく-1を入力したときに、自分のことわざを入力して出力できるようにすることです。

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);
        }
    }
}

}

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

このswitchcaseは、「-1」と入力して発言を入力し、それをフレーズに書き込むときに尋ねます。switch caseの後、上記のコードのように進めることができます。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ