본문 바로가기
프로그래머/코딩(C#)

C#_Class[Question]

by FourthWay 2023. 5. 4.
728x90
반응형

[Question]

1. 무한루프를 적용해서 계속 반복시킵니다.

2. Exit를 선택하면 프로그램이 종료됩니다.

 

using System;

namespace _29_class
{
 internal class Restaurant
 {
  private string[] foods = {"냉면", "칼국수", "쌀국수", "떡볶이", "순대", "튀김", "Exit"};
  private int select;
  
  public void QuestionOrder()
  {
   Console.WriteLine();
   Console.WriteLine("==========================");
   for(int i=0; i<foods.Length;i++)
      Console.WriteLine("{0}. {1}", i, foods[i]);
  }
  
  public int SelectFood()
  {
   Console.WriteLine("-------------------------");
   Console.WriteLine("어느 음식을 선택하실래요?");
   select = Int32.Parse(Console.WriteLine());
   return select;
  }
  
  public void DeliveryFood()
  {
   Console.WriteLine("{0}이 나왔습니다", foods[select]);
   Console.WriteLine("맛있게 드세요~");
  }
 }
 
 internal class Program
 {
  static void Main(string[] args)
  {
   Restaurant rest = new Restaurant();
   while(true)
   {
    rest.QuestionOrder();
    int num = rest.SelectFood();
    if(num == 6)
    	break;
    rest.DeleveryFood();
   }
   
   Console.WriteLine("안녕히 계세요~");
  }
 }
}

private string[] foods = {"냉면", "칼국수", "쌀국수",
                        "떡볶이", "순대", "튀김", "Exit"};
        private int select;

 

*선택할 수 있는 메뉴의 종류를 배열을 이용해 나열한다.

 

public void QuestionOrder()
        {            
            Console.WriteLine();
            Console.WriteLine("=============================");
            for(int i=0;i<foods.Length;i++)
                Console.WriteLine("{0}. {1}", i, foods[i]);
        }

*콘솔창에 0번째인 냉면부터 6번째 exit 까지 (전체를 배열하기 위해 Lenght를 씀) 나타낸다.

 

public int SelectFood()
        {
            Console.WriteLine("----------------------");
            Console.WriteLine("어느 음식을 선택하실래요?");
            select = Int32.Parse(Console.ReadLine());
            return select;
        }

*주문을 받을 차례이다. 주문할 음식의 번호를 선택한다.

 

public void DeliveryFood()
        {
            Console.WriteLine("{0}이 나왔습니다", foods[select]);
            Console.WriteLine("맛있게 드세요~");
        }

* 선택한 음식이 나왔습니다.

맛있게 드세요~

 

 

static void Main(string[] args)
        {
            Restaurant rest = new Restaurant();
            while (true)
            {
                rest.QuestionOrder();
                int num = rest.SelectFood();
                if (num == 6)
                    break;
                rest.DeliveryFood();
            }

            Console.WriteLine("안녕히 계세요~");
        }

 

*모든 명령을 실행할 Main에 클래스를 모두 올려준다.

QuestionOrder()

SelectFood()

DeliveryFood()

 

만약 6번 exit를 선택하였을 경우 break; 를 써서 프로그램이 끝이 나게 되고

안녕하 가세요 를 출력한다.

728x90
반응형

'프로그래머 > 코딩(C#)' 카테고리의 다른 글

_28_array_2  (0) 2023.05.04
_27_array_1  (1) 2023.05.02
_26_array  (0) 2023.05.02
_25_function_question  (0) 2023.05.01
_24_function  (1) 2023.04.28