[프로그램]

C#으로 2중 라인 엘리베이터

Blackberrys 2023. 6. 26. 17:26
반응형

C#으로 2중 라인 엘리베이터 프로그램.
엘리베이터의 동작을 시뮬레이션하는 프로그램입니다.

csharpCopy code
using System;
class ElevatorProgram
{
static void Main(string[] args)
{
Elevator elevator1 = new Elevator(1);
Elevator elevator2 = new Elevator(2);
while (true)
{ Console.WriteLine("1. 엘리베이터 1 호출");
Console.WriteLine("2. 엘리베이터 2 호출");
Console.WriteLine("3. 종료");
Console.Write("선택: ");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Write("목적지 층수 입력 (1-10): ");
int floor1 = int.Parse(Console.ReadLine());
elevator1.Move(floor1);
break;
case 2:
Console.Write("목적지 층수 입력 (1-10): ");
int floor2 = int.Parse(Console.ReadLine());
elevator2.Move(floor2);
break;
case 3:
Console.WriteLine("프로그램을 종료합니다.");
return;
default:
Console.WriteLine("잘못된 선택입니다. 다시 선택해주세요.");
break;
}
}
}
}
class Elevator
{
private int id;
private int currentFloor;
public Elevator(int id)
{
this.id = id;
this.currentFloor = 1;
}
public void Move(int destinationFloor)
{
Console.WriteLine($"엘리베이터 {id} 이동 시작 - 현재 층: {currentFloor}");
if (destinationFloor > currentFloor)
{
for (int floor = currentFloor; floor <= destinationFloor; floor++)
{
Console.WriteLine($"엘리베이터 {id} 이동 중 - 현재 층: {floor}");
}
}
else if (destinationFloor < currentFloor)
{
for (int floor = currentFloor; floor >= destinationFloor; floor--)
{
Console.WriteLine($"엘리베이터 {id} 이동 중 - 현재 층: {floor}");
}
}
currentFloor = destinationFloor; Console.WriteLine($"엘리베이터 {id} 이동 완료 - 도착 층: {currentFloor}");
}
}

이 코드는 두 개의 엘리베이터를 생성하고, 각 엘리베이터에 대한 호출을 받아 목적지로 이동하는 기능을 구현합니다. 프로그램을 실행하면 사용자로부터 엘리베이터 호출 및 목적지 입력을 받을 수 있습니다. 엘리베이터가 이동하는 동안 현재 층수를 표시하며, 목적지에 도착하면 도착 층수를 출력합니다.
위의 예시 코드를 Visual Studio 등의 C# 개발 환경에서 실행하면 됩니다.