Exception handling 异常处理代码中存在非法表达式错误

Exception handling 异常处理代码中存在非法表达式错误,exception-handling,freepascal,Exception Handling,Freepascal,我正在写一个用FreePascal计算交通罚款的小程序。源代码如下: program TrafficFine; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes,SysUtils; var userInput : Char; Fine : Integer; TotalFine : Integer; Day

我正在写一个用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.
programmtrafficfine;
{$mode objfpc}{$H+}
使用
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
类,sysutil;
变量
用户输入:Char;
精细:整数;
TotalFine:整数;
DaysPassed:整数;
FineType:整数;
开始
用户输入:='y';
而(userInput='Y')或(userInput='Y')则
开始;
writeln('输入罚款类型:');
writeln(“-输入1表示未系安全带”);
writeln('-输入2表示无驾照驾驶”);
writeln('-输入3表示超速行驶');
尝试
写入('输入值:');
readln(FineType);
如果是(第3类),则
引发异常。创建('超出范围的精细类型');
箱型
1:罚款:=500;
2:罚款:=1000;
3:罚款:=2000;
除了
关于e:exception do{line 39}
开始
Writeln('错误:'+e.message);
继续;
结束;
写入('输入自罚款后经过的天数:');
readln(DaysPassed);

如果daysPassed10)和(daysPassed似乎您忘了用结尾关闭Case;

似乎您忘了用结尾关闭Case;

您的代码中有几个缺陷,我在源代码中进行了更正和注释。请尝试此新版本

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. 
programmtrafficfine;
{$mode objfpc}{$H+}
使用
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
类,sysutil;
变量
用户输入:Char;
精细:整数;
TotalFine:整数;
DaysPassed:整数;
FineType:整数;
开始
用户输入:='y';
而(userInput='Y')或(userInput='Y')则
开始//删除分号
writeln('输入罚款类型:');
writeln(“-输入1表示未系安全带”);
writeln('-输入2表示无驾照驾驶”);
writeln('-输入3表示超速行驶');
尝试
写入('输入值:');
readln(FineType);
如果是(第3类),则
引发异常。创建('超出范围的精细类型');
箱型
1:罚款:=500;
2:罚款:=1000;
3:罚款:=2000;
end;//添加了end;
除了
关于e:exception do{line 39}
开始
Writeln('错误:'+e.message);
继续;
结束;
end;//添加了end;
写入('输入自罚款后经过的天数:');
readln(DaysPassed);

如果daysPassed10)和(daysPassed您的代码中有几个缺陷,我会在源代码中进行更正和注释。试试这个新版本

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. 
programmtrafficfine;
{$mode objfpc}{$H+}
使用
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
类,sysutil;
变量
用户输入:Char;
精细:整数;
TotalFine:整数;
DaysPassed:整数;
FineType:整数;
开始
用户输入:='y';
而(userInput='Y')或(userInput='Y')则
开始//删除分号
writeln('输入罚款类型:');
writeln(“-输入1表示未系安全带”);
writeln('-输入2表示无驾照驾驶”);
writeln('-输入3表示超速行驶');
尝试
写入('输入值:');
readln(FineType);
如果是(第3类),则
引发异常。创建('超出范围的精细类型');
箱型
1:罚款:=500;
2:罚款:=1000;
3:罚款:=2000;
end;//添加了end;
除了
关于e:exception do{line 39}
开始
Writeln('错误:'+e.message);
继续;
结束;
end;//添加了end;
写入('输入自罚款后经过的天数:');
readln(DaysPassed);
如果daysPassed 10)和(daysPassed