StartprogrammingusingObjectPascal:RestaurantProgramUsingRepeatLoop

From 흡혈양파의 번역工房
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

repeat 순환문을 사용한 음식점 프로그램

var
    Selection: Char;
    Price: Integer;
    Total: Integer;
begin
    Total:= 0;
    repeat
        Writeln('Welcome to Pascal Restaurant. Please select your order');
        Writeln('1 - Chicken           (10 Geneh)');
        Writeln('2 - Fish                  (7 Geneh)');
        Writeln('3 - Meat                (8 Geneh)');
        Writeln('4 - Salad                (2 Geneh)');
        Writeln('5 - Orange Juice (1 Geneh)');
        Writeln('6 - Milk                  (1 Geneh)');
        Writeln('X - nothing');
        Writeln;
        Write('Please enter your selection: ');
        Readln(Selection);

        case Selection of
            '1': begin
                Writeln('You have ordered Chicken, this will take 15 minutes');
                Price:= 10;
            end;
            '2': begin
                Writeln('You have ordered Fish, this will take 12 minutes');
                Price:= 7;
            end;
            '3': begin
                Writeln('You have ordered meat, this will take 18 minutes');
                Price:= 8;
            end;
            '4': begin
                Writeln('You have ordered Salad, this will take 5 minutes');
                Price:= 2;
            end;
            '5': begin
                Writeln('You have ordered Orange juice, this will take 2 minutes');
                Price:= 1;
            end;
            '6': begin
                Writeln('You have ordered Milk, this will take 1 minute');
                Price:= 1;
            end;
            else
            begin
                Writeln('Wrong entry');
                Price:= 0;
            end;
        end;

        Total:= Total + Price;

    until (Selection = 'x') or (Selection = 'X');
    Writeln('Total price = ', Total);
    Write('Press enter key to close');
    Readln;
end.


앞의 예제에서 이 기법들을 사용했습니다.

  1. case 분기에 begin end 를 더하여 여러개의 구문을 하나의 구문으로 만들었습니다.
  2. 주문의 총 가격을 더하기 위해 변수 Total 값을 0(변수에 초기값 위치)으로 초기화했습니다. 그 다음 선택한 가격을 매 순환마다 Total변수에 더했습니다.
    Total:= Total + Price;
    
  3. 주문을 끝내기 위해 대문자 'X' 와 소문자 'x' 두가지 옵션을 넣었습니다. 두 문자는 컴퓨터 메모리에서 다르게 표현(저장)합니다.


참고

아래 줄을

until (Selection = 'x') or (Selection = 'X');


다음 코드처럼 줄여서 사용 할 수 있습니다

until UpCase(Selection) = 'X';


이는 Selection 변수의 값이 소문자라면 대문자로 바꿔줄 것이고, (x또는 X) 두 경우에 대해 True를 되돌릴 것입니다.