以数字和字符串形式读取变量
Reading A Variable As Both A Number And String of Words
我试图让 Dep_Code
在选择给定的选项(1、2 或 3)后读取为字符串。我首先在我的第一个程序中将它设置为整数(我认为)并且能够让它读出作为单词给出的选项(帐户 ACC
或其他)。然而,它被不小心删除了。我尝试了各种方法来获取它,甚至将 Dep_Code
设置为字符串,但它不起作用,而且我不断收到各种错误。顺便说一句,我不熟悉编程,所以我知道下面的代码很不正确……但我希望大家能帮忙。谢谢!
REPEAT
writeln ('Please enter the Department Code:- ');
writeln;
writeln ('1. Accounts (ACC)');
writeln ('2. Human Resources (HR)');
writeln ('3. Operations (OP)');
writeln;
readln (Dep_Code);
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
ELSE IF Dep_Code = 2 THEN
Dep_Code := ('Human Resources(HR)')
ELSE IF Dep_Code = 3 THEN
Dep_Code := ('Operations (OP)');
UNTIL ((Dep_Code >= 1) AND (Dep_Code <= 3));
这是不可能的。 Pascal 是一种严格类型的语言,某些东西不能同时是整数 和 字符串,变量也不能改变类型:
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
但是你根本不需要字符串。保持整数。如有必要,处理各个部门的功能可以编写或定义此类字符串。您的菜单逻辑不需要字符串变量。
做类似的事情:
procedure HandleAccounts(var Error: Boolean);
begin
...
end;
// Skipped the other functions to keep this answer short ...
var
Dep_Code: Integer;
AllFine: Boolean;
// Skip the rest of the necessary code ...
repeat
// Skipped the Writelns to keep this answer short ...
Readln(Dep_Code);
Error := False;
case Dep_Code of
1: HandleAccounts(Error);
2: HandleHumanResources(Error);
3: HandleOperations(Error);
else
Error := True;
end;
until not Error;
上面,我跳过了一些代码。我想你可以填空。
我试图让 Dep_Code
在选择给定的选项(1、2 或 3)后读取为字符串。我首先在我的第一个程序中将它设置为整数(我认为)并且能够让它读出作为单词给出的选项(帐户 ACC
或其他)。然而,它被不小心删除了。我尝试了各种方法来获取它,甚至将 Dep_Code
设置为字符串,但它不起作用,而且我不断收到各种错误。顺便说一句,我不熟悉编程,所以我知道下面的代码很不正确……但我希望大家能帮忙。谢谢!
REPEAT
writeln ('Please enter the Department Code:- ');
writeln;
writeln ('1. Accounts (ACC)');
writeln ('2. Human Resources (HR)');
writeln ('3. Operations (OP)');
writeln;
readln (Dep_Code);
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
ELSE IF Dep_Code = 2 THEN
Dep_Code := ('Human Resources(HR)')
ELSE IF Dep_Code = 3 THEN
Dep_Code := ('Operations (OP)');
UNTIL ((Dep_Code >= 1) AND (Dep_Code <= 3));
这是不可能的。 Pascal 是一种严格类型的语言,某些东西不能同时是整数 和 字符串,变量也不能改变类型:
IF Dep_Code = 1 THEN
Dep_Code := ('Accounts (ACC)')
但是你根本不需要字符串。保持整数。如有必要,处理各个部门的功能可以编写或定义此类字符串。您的菜单逻辑不需要字符串变量。
做类似的事情:
procedure HandleAccounts(var Error: Boolean);
begin
...
end;
// Skipped the other functions to keep this answer short ...
var
Dep_Code: Integer;
AllFine: Boolean;
// Skip the rest of the necessary code ...
repeat
// Skipped the Writelns to keep this answer short ...
Readln(Dep_Code);
Error := False;
case Dep_Code of
1: HandleAccounts(Error);
2: HandleHumanResources(Error);
3: HandleOperations(Error);
else
Error := True;
end;
until not Error;
上面,我跳过了一些代码。我想你可以填空。