2012-04-18 6 views
0

FreePascal에서 트래픽 과태를 계산하기위한 작은 프로그램을 작성하고 있습니다.예외 처리 코드에서 잘못된 표현 오류가 발생했습니다.

program TrafficFine; 

{$mode objfpc}{$H+} 

uses 
{$IFDEF UNIX}{$IFDEF UseCThreads} 
cthreads, 
{$ENDIF}{$ENDIF} 
Classes,SysUtils; 

var 
    userInput : Char; 
    Fine  : Integer; 
    TotalFine : Integer; 
    DaysPassed: Integer; 
    FineType : Integer; 

begin 

    userInput := 'y'; 

    while (userInput = 'Y') or (userInput = 'y') do 
    begin; 
     writeln('Enter type of fine:'); 
     writeln('- Enter 1 for not wearing a seat-belt.'); 
     writeln('- Enter 2 for driving without a license'); 
     writeln('- Enter 3 for over-speeding.'); 

     try 
      write('Enter value: '); 
      readln(FineType); 
      if(FineType <0) or (FineType>3) then 
       raise exception.Create('Fine type outside of range.'); 
      case FineType of 
      1: Fine:= 500; 
      2: Fine:= 1000; 
      3: Fine:= 2000; 
     except 
     on e: exception do {line 39} 
     begin 
      Writeln('Error: '+e.message); 
      continue; 
     end; 

     write('Enter number of days passed since fine: '); 
     readln(DaysPassed); 
     if daysPassed<=10 then 
      TotalFine := Fine; 
     else if (daysPassed >10) and (daysPassed <=30) then 
      TotalFine := Fine * 2; 
     else 
      TotalFine := Fine*2 + Fine*0.5; 

     writeln('Total Fine is ' + IntToStr(TotalFine));   
     writeln('Would you like to calculate another fine: '); 
     readln(userInput); 
    end; 
end. 

을 나는 다음과 같은 오류를 얻을 : 소스 코드는 다음과 같습니다

Free Pascal Compiler version 2.4.4-2ubuntu1 [2011/09/27] for i386 Copyright (c) 1993-2010 by Florian Klaempfl Target OS: Linux for i386 Compiling /home/ubuntu/Desktop/TrafficFine.pas TrafficFine.pas(39,3) Error: Illegal expression TrafficFine.pas(40,3) Error: Constant Expression expected TrafficFine.pas(40,3) Fatal: Syntax error, ":" expected but "identifier ON" found Fatal: Compilation aborted

나는 그래서 내가 잘못 갔어요 어디 잘 모르겠어요 어떤 책에서 예를 따랐다. 어떤 도움을 주시면 감사하겠습니다. 감사.

답변

4

코드에 몇 가지 결함이 있습니다. 소스에서 수정하고 주석 처리했습니다. 이 새로운 버전을 사용해보십시오.

program TrafficFine; 

{$mode objfpc}{$H+} 

uses 
{$IFDEF UNIX}{$IFDEF UseCThreads} 
cthreads, 
{$ENDIF}{$ENDIF} 
Classes,SysUtils; 

var 
    userInput : Char; 
    Fine  : Integer; 
    TotalFine : Integer; 
    DaysPassed: Integer; 
    FineType : Integer; 

begin 

    userInput := 'y'; 

    while (userInput = 'Y') or (userInput = 'y') do 
    begin //removed semicolon 
     writeln('Enter type of fine:'); 
     writeln('- Enter 1 for not wearing a seat-belt.'); 
     writeln('- Enter 2 for driving without a license'); 
     writeln('- Enter 3 for over-speeding.'); 

     try 
      write('Enter value: '); 
      readln(FineType); 
      if(FineType <0) or (FineType>3) then 
       raise exception.Create('Fine type outside of range.'); 
      case FineType of 
      1: Fine:= 500; 
      2: Fine:= 1000; 
      3: Fine:= 2000; 
      end;//added end; 
     except 
     on e: exception do {line 39} 
     begin 
      Writeln('Error: '+e.message); 
      continue; 
     end; 
     end; //added end; 

     write('Enter number of days passed since fine: '); 
     readln(DaysPassed); 
     if daysPassed<=10 then 
      TotalFine := Fine //removed semicolon 
     else if (daysPassed >10) and (daysPassed <=30) then 
      TotalFine := Fine * 2 //removed semicolon 
     else 
      TotalFine := (Fine*2) + (Fine div 2);//replaced this sentence (Fine*2) + (Fine*0.5) 

     writeln('Total Fine is ' + IntToStr(TotalFine)); 
     writeln('Would you like to calculate another fine: '); 
     readln(userInput); 
    end; 
end. 
1

마지막 사례가있는 것을 잊어 버린 것 같습니다.

관련 문제