Pascal重复程序直到字符串变量为空

时间:2016-01-24 20:45:29

标签: pascal

所以我正在为pascal编写一个程序,它要求我重复该程序,直到输入无名称This Is the exact question if it helps

所以我发现pascal中null / empty的含义是''我尝试了不同的代码,这是我试过的最后一个

program Lunch;
Const
  Dues_Percentage1=0.3;
    Dues_Percentage2=0.2;
    Var
      Name:String;
      Age:Integer;
      Lunch_Money:Real;
      Dues:Real;
begin
  While Name <> '' Do
  Writeln('Please Enter Your Name');
  Read(Name);
  Writeln('Please Enter Your Age');
  Read(Age);
  Writeln('Please Enter The Amount of Money You Receive');
  Read(Lunch_Money);
  If Age >10 then
  Dues:= Lunch_Money*Dues_Percentage1
  else
  Dues:= Lunch_Money*Dues_Percentage2;
  Writeln('The Amount Of Lunch Money You Receive is $',Lunch_Money:4:2);
  WriteLn('The Amount Of Dues You will pay Is $',Dues:3:2);
  Readln(Dues,Lunch_Money);
end.

它不起作用我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:0)

问题是如果你使用while循环,你需要初始化&#34; Name&#34;在进行测试之前(然后在循环中的每个下一个测试之前读取它)。例如:

 var
   Name : string;
 begin
   //Initialise Name
   readln(Name);
   while Name <> '' do
   begin
     // do stuff here
     // and here
     //then read the next name
     readln(Name);
   end;
 end;

请注意,虽然只执行下一个语句(如果条件为真),所以如果你希望while只做一个语句,你需要使用复合语句。

如果您使用重复,直到您可能需要稍微不同的结构:

 var
   Name : string;
 begin
   repeat
     readln(Name);
     if Name <> '' then
     begin
        // do stuff here
        // and here
     end;
   until Name = '';
 end;
相关问题