我该如何修复此语法错误?

时间:2018-02-28 01:24:16

标签: windows pascal freepascal pascalscript

我想测试我的pascal代码,但我无法弄清楚为什么会出现这个错误:

  

致命:语法错误,;预期但是ELSE找到了

Program Lesson2_Program1;
Var
   name, surname, community, inputAnswer : String;
   GrossSalary, expenses, NetIncome: Integer;

Begin
    write('Enter your name:');
    readln(name);
    writeln;{new line}
    writeln;{new line}
    writeln('Please proceed to enter your gross salary: ',name);
    Begin {no semicolon}
    Write('Input applicant GrossSalary:');
    Readln(GrossSalary);
    Writeln('Input applicant expenses:');
    Readln(expenses);
    NetIncome := GrossSalary - expenses; {subtraction};
    Writeln(NetIncome);
    writeln;{new line}
    writeln('Please select a community (ClarendonC/SangreGV/ProvidenceG)');
    readln(InputAnswer);
Begin
    If (InputAnswer = 'ClarendonC') Then
    If NetIncome>=12500 Then
    Writeln('congragultions you qualify for this community');
    Else
    Writeln('Sorry, you do not qualify for the chosen community');
End.
    Else if (InputAnswer = 'SangreGV') Then
    If NetIncome>=9500 Then
    If NetIncome<=12500 Then
    Write('congragulations you qualify for this community');
    Else
    Write('Sorry, you do not qualify for the chosen community');
End.

1 个答案:

答案 0 :(得分:1)

您的代码存在各种问题。如果你正确地缩进它,问题会更明显:

Program Lesson2_Program1;

Var
   name, surname, community, inputAnswer : String;
   GrossSalary, expenses, NetIncome: Integer;

Begin
  write('Enter your name:');
  readln(name);
  writeln;{new line}
  writeln;{new line}
  writeln('Please proceed to enter your gross salary: ',name);
  Begin {no semicolon}
    Write('Input applicant GrossSalary:');
    Readln(GrossSalary);
    Writeln('Input applicant expenses:');
    Readln(expenses);
    NetIncome := GrossSalary - expenses; {subtraction};
    Writeln(NetIncome);
    writeln;{new line}
    writeln('Please select a community (ClarendonC/SangreGV/ProvidenceG)');
    readln(InputAnswer);
    Begin
      If (InputAnswer = 'ClarendonC') Then
      { ERROR! Missing BEGIN }
        If NetIncome>=12500 Then
          Writeln('congragultions you qualify for this community'); { ERROR! Erroneous ';' }
        Else
          Writeln('Sorry, you do not qualify for the chosen community');
      End. { ERROR! END without BEGIN, and Erroneous '.' }
      Else if (InputAnswer = 'SangreGV') Then
      { WARNING! Missing BEGIN }
        If NetIncome>=9500 Then
          If NetIncome<=12500 Then
            Write('congragulations you qualify for this community'); { ERROR! Erroneous ';' }
          Else
            Write('Sorry, you do not qualify for the chosen community');
        { WARNING! Missing ELSE ... END; for NetIncome < 9500 }
      { WARNING! Missing END; }
      { WARNING! Missing ELSE for 'ProvidenceG' }
    { ERROR! Missing END; }
  { ERROR! Missing END; }
End.

正确的代码看起来应该更像这样:

Program Lesson2_Program1;

Var
   name, surname, community, inputAnswer : String;
   GrossSalary, expenses, NetIncome: Integer;

Begin
  Write('Enter your name: ');
  Readln(name);
  Writeln;{new line}
  Writeln;{new line}
  Writeln('Please proceed to enter your gross salary, ', name);
  Write('Input applicant GrossSalary: ');
  Readln(GrossSalary);
  Writeln('Input applicant expenses: ');
  Readln(expenses);
  NetIncome := GrossSalary - expenses; {subtraction}
  Writeln(NetIncome);
  Writeln;{new line}
  Writeln('Please select a community (ClarendonC/SangreGV/ProvidenceG)');
  Readln(InputAnswer);
  If (InputAnswer = 'ClarendonC') Then
  Begin
    If NetIncome >= 12500 Then
      Writeln('Congratulations, you qualify for this community')
    Else
      Writeln('Sorry, you do not qualify for the this community');
  End
  Else if (InputAnswer = 'SangreGV') Then
  Begin
    If (NetIncome >= 9500) and (NetIncome <= 12500) Then
      Write('Congratulations, you qualify for this community')
    Else
      Write('Sorry, you do not qualify for this community');
  End
  Else if (InputAnswer = 'ProvidenceG') Then
  Begin
    //...
  End
  Else
    Write('Sorry, you entered an invalid community');
  End;
End.