【问题标题】:How can i fix my this syntax error? [closed]我怎样才能修复我的这个语法错误? [关闭]
【发布时间】:2018-02-28 01:24:16
【问题描述】:

我想测试我的 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.

【问题讨论】:

  • 代码图像绝对没用。请参阅this Meta post,了解您应该避免发布图片的众多原因列表。代码和错误以文本形式出现,应按原样发布。当您在此处创建帐户时,建议您使用 tour 并阅读 help center 页面以熟悉此站点。请在此处发布您的下一个问题之前这样做,尤其是 How to Askminimal reproducible example
  • 感谢您的编辑。如果你学会正确缩进你的代码,并匹配beginend语句,并知道end.(结尾后跟一个点)表示代码的结尾,这将对你有很大帮助。
  • 另外,在 Pascal 中,在 else 之前从不分号用于 if。检查你的 Pascal 语言手册。
  • 非常感谢,我无法表达我对此的感激之情
  • 嗯...我已经看到至少一个非常相似的问题,很久以前发布的。没有回答吗?

标签: windows pascal freepascal pascalscript


【解决方案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.

【讨论】:

  • 哦,我的上帝保佑您,先生,感谢您抽出宝贵的时间,很抱歉我无法正确缩进代码,但我一定会阅读帮助中心页面
  • 您可以正确缩进代码。你只是没有。请注意,正确的格式不仅对问题的读者很重要,对您也很重要,因为您仍然可以在 6 个月后阅读它,并且它显示出逻辑和语法方面的缺陷,例如错误的开头-end 块等。格式化并不奢侈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多