Pascal代码报错:预期分号却识别到标识符SALARY,求修复
Pascal薪资计算程序语法错误修复
核心错误原因
你遇到的编译错误根源是**while循环未用begin...end包裹循环体**。Pascal规定,当循环体包含多条语句时,必须用begin和end将整个循环代码块包裹,否则编译器会把while后的第一条语句单独视为循环体,后续语句会被判定为循环外的无效代码,引发语法冲突。
具体修复步骤
1. 补全while循环的块结构
在while Running = TRUE do后添加begin,在循环体最后(if Enter = 2 then ... end之后)添加end;,将所有循环内代码包裹起来:
Running := TRUE ; GrossIncome := 0 ; while Running = TRUE do begin // 新增循环块起始标记 writeln ('please input employees name' ); readln(Nameemp); // ... 所有循环内代码 ... if Enter = 2 then begin Running := FALSE; end; end; // 新增循环块结束标记
2. 修正变量名拼写错误
输出职位时误用了Position,但变量定义为Pos,需统一:
writeln(Pos); // 替换原代码中的writeln(Position);
3. 常量名大小写统一
Pascal对大小写敏感,你定义的常量是Com1、Com2,但代码中写成了小写的com1、com2,需修正为大写:
ComAmt := Com1 * TotalSales ; // 替换原代码中的com1 // ... ComAmt := Com2 * TotalSales ; // 替换原代码中的com2
4. 修正姓名变量类型
Nameemp定义为char只能存储单个字符,无法录入完整姓名,需改为string类型:
var Running : Boolean ; Nameemp : string ; // 替换原char类型定义 Pos : char ; // ... 其他变量定义 ...
修复后的完整代码
program Hello; const Allowance = 0.05 ; Deduction = 0.215 ; Com1 = 0.1 ; Com2 = 0.05 ; ComReq = 8000.00; var Running : Boolean ; Nameemp : string ; Pos : char ; BasicSalary : real ; Salary : real ; Code : char ; AllAmt : real ; DedAmt : real ; GrossIncome : real ; TotalSales : real ; ComAmt : real ; Enter : integer ; begin Running := TRUE ; GrossIncome := 0 ; while Running = TRUE do begin writeln ('please input employees name' ); readln(Nameemp); writeln ('please input employees position' ); readln(Pos); writeln ('enter employee’s category code'); readln(Code); writeln('please enter the employees basic salary'); readln(BasicSalary); AllAmt := BasicSalary * Allowance; DedAmt := BasicSalary * Deduction; GrossIncome := BasicSalary + AllAmt; Salary := GrossIncome - DedAmt; if Code = 'SS' then begin writeln ('please enter employees total sales for the month'); readln(TotalSales); if TotalSales >= ComReq then begin ComAmt := Com1 * TotalSales ; end else begin ComAmt := Com2*TotalSales ; end Salary := Salary + ComAmt; end else begin writeln('employee is not applicable for a commision'); end; writeln(Nameemp); writeln(Pos); writeln(GrossIncome); writeln(Salary); if Code = 'SS' then begin writeln(ComAmt); end; writeln('would you like to run the program again. Enter 1 for yes and enter 2 for no'); readln(Enter); if Enter = 2 then begin Running := FALSE; end; end; end.
额外注意事项
- Pascal中语句结尾的分号需正确添加,比如
else块内的writeln语句末尾要补充分号; - 若需要录入完整职位名称,建议将
Pos变量也改为string类型,避免只能输入单个字符的限制。
内容的提问来源于stack exchange,提问作者Shernan Jankie
相关产品推荐
相关产品推荐

