728x90

  Random random = new Random();

                string[] Color = { "yellow", "blue", "red", "green" };
               

                int item = random.Next(Color.Length); // in method
                string col = Color[item];

                Console.WriteLine("You have " + col );



class Program

{
    static Random random = new Random();
    public static void Main()

    {

        Penguin penguin = new Penguin("Winter", "squawk", Penguin.food[GetRandomNumber(Penguin.food.Length)]);

        Console.WriteLine(penguin.About());

        Console.ReadKey();

    }

 
    public static int GetRandomNumber(int range)

    {

        return random.Next(range);

    }

 
}

 

class Penguin

{

    string Name;

    string Noise;

    string Food;

    public static string[] food = new string[] {"fish", "a", "b", "c", "d", "e" };

 

    public Penguin(string name, string noise, string food)

    {

        Name = name;

        Noise = noise;

        Food = food;

    }

 

    public string About()

    {

        return $"The penguin {Name} says {Noise} when given {Food}";

    }

 

}

 

'💻 소프트웨어 Tutorial 모음 > C# 비주얼스튜디오' 카테고리의 다른 글

자주쓰이는 기본코드, C# 기초  (0) 2021.07.19
Dictionary, c#, 예문  (0) 2021.07.19
배열문, Array, C#  (0) 2021.07.19
기초! C# 콘솔파일띄우기  (0) 2021.07.19
조건문(if, else)  (0) 2021.07.19