본문 바로가기

c#(유데미)

C#:객체 Car클래스 메서드 이용하기/배열arrays(3D차원)

1. Car 객체 이용해서 Car클래스에 있는 메서드 이용하기:매개변수 이용X

//Car.cs
using System;
namespace Hello_World
{
	internal class Car
	{
		//Constructor
		public Car()
		{
			Console.WriteLine("Car was created");
		}
		public void Drive() {
			Console.WriteLine("Car is driving");
		}

		public void Stop()
		{
			Console.WriteLine("Car is stopped");
		}
	}
}
//practice.cs
using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            Car audi = new Car();
            audi.Drive();

            Console.WriteLine("press 1 to stop the car");
            string userInput = Console.ReadLine();
            if(userInput == "1")
            {
                audi.Stop();
            }
            else
            {
                Console.WriteLine("Car drives indefinetely");
            }
        }


    }
}

Practice.cs파일을 실행하면 결과값

 

 

 

2. Car 객체 이용해서 Car클래스에 있는 메서드 이용하기(심화): 매개변수 이용O

  ->색깔,차이름

//Car.cs
using System;
namespace Hello_World
{
	internal class Car
	{
		private string _name; //private field typically used for storing data.
		private int _hp;
		private string _color;
		//Constructor
		public Car(string name, int hp = 0, string color = "black")
		{
			_name = name;
			Console.WriteLine(name + " Car was created");
			_hp = hp;
			_color = color;
		}
		public void Drive() {
			Console.WriteLine(_name + " is driving");
		}

		public void Stop()
		{
			Console.WriteLine(_name + " is stopped");
		}

		public void Details()
		{
			Console.WriteLine("The " + _color + " car " + _name + " has " + _hp);
		}
	}
}
//practice.cs
using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            Car audi = new Car("audi A4",250, "blue");
            audi.Drive();
            audi.Details();
            Car bmw = new Car("BMW M5", 350);
            bmw.Drive();
            bmw.Details();

            Console.WriteLine("press 1 to stop the car");
            string userInput = Console.ReadLine();
            if(userInput == "1")
            {
                audi.Stop();
            }
            else
            {
                Console.WriteLine("Car drives indefinetely");
            }
        }


    }
}

Practice.cs파일을 실행하면 결과값

 

using System;
namespace Hello_World
{
	internal class Car
	{
		//Member variables
		private string _name; //private field typically used for storing data.
		private int _hp;
		private string _color;
        //Default Constructor
		public Car()
		{
			_name = "Car";
			_hp = 0;
			_color = "Red";
		}

        //Partial specification Constructor
        public Car(string name, int hp = 0)
		{
			_name = name;
			Console.WriteLine(name + " Car was created");
			_hp = hp;
			_color = "red";
		}
		//Full Specification Constructor
        public Car(string name, int hp = 0, string color)
        {
            _name = name;
            Console.WriteLine(name + " was created");
            _hp = hp;
            _color = color;
        }
        public void Drive() {
			Console.WriteLine(_name + " is driving");
		}

		public void Stop()
		{
			Console.WriteLine(_name + " is stopped");
		}

		public void Details()
		{
			Console.WriteLine("The " + _color + " car " + _name + " has " + _hp);
		}
	}
}

 

 

 

 

Arrays배열

using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] grades = new int[5];

            grades[0] = 20;
            grades[1] = 15;
            grades[2] = 12;
            grades[3] = 9;
            grades[4] = 7;

            Console.WriteLine("grades at index 0 : {0}", grades[0]);

            string input = Console.ReadLine();
            //assign value to array grades at index 0

            grades[0] = int.Parse(input);
            Console.WriteLine("grades at index 0 : {0}", grades[0]);
            Console.ReadKey();

            //another way of initializing an array
            int[] gradesOfMathStudentsA = { 20, 14, 12, 8, 8 };
            //third way of initializing an array
            int[] gradesOfMathStudentsB = new int[] { 15, 11, 3, 18, 15 };

            Console.WriteLine("lengtht of gradesOfMathStudentsA: {0}", gradesOfMathStudentsA.Length);
            Console.ReadKey();

        }


    }
}

콘솔 결과값

 

 

 

 

3d차원 배열 array

using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare 2D Array
            string[,] matrix;

            //3D Array
            int[,,] threeD;

            //two dimensional array
            int[,] array2D = new int[,]
            {
                {1,2,3}, //row 0
                {4,5,6}, //row 1
                {7,8,9} //row 2
            };

            string[,,] array3D = new string[,,]
            {
                {
                    {"000", "001"},
                    {"010", "011"},
                    {"Hi there", "What's up?"}
                },
                {
                    {"100", "101"},
                    {"110", "111"},
                    {"Another one", "Last entry"}
                }
            };

            Console.WriteLine("The value is {0}", array3D[1,2,1]);
            Console.ReadKey();
        }


    }
}

콘솔 결과값

 

 

//Rank 1차원인지 , 2차원인지 , 3차원인지 알려줌

//Rank 1차원인지 , 2차원인지 , 3차원인지 알려줌
string[,] array2DString = new string[3, 2]
            {
                { "one", "two" },
                { "three", "four" },
                { "five", "six" }
            };

            array2DString[1, 1] = "chicken";

            int dimensions = array2DString.Rank; //1차원인지 , 2차원인지 , 3차원인지 알려.

            Console.WriteLine("The value is {0}", array2DString[1, 1]);
            Console.WriteLine("The value is {0}", dimensions);
            Console.ReadKey();