You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ada编程:实现输入'T'/'F'对应布尔类型输出True/False

Ada记录类型中布尔值的输入输出自定义实现(不修改布尔成员初始化)

要实现输入'T'对应布尔值True(输出"True")、输入'F'对应布尔值False(输出"False"),且不修改T: Boolean:= False;这条初始化语句,你需要修改Get_3和Put_3两个过程,具体改动如下:

修改后的完整代码(仅改动Get_3和Put_3)

type Sub_J is 
  record
     Y: Character:= '9';
     Q: Character:= 'p';
  end record; 

type Sub_B is
  record
     Y: Character:= 'J';
     Q: Character:= 'o';
  end record;

type Sub_O is 
  record
     T: Boolean:= False; -- 保留原初始化语句,未修改
     L: Character:= '5';
  end record;

type DS3 is 
  record
     J: Sub_J;
     B: Sub_B;
     O: Sub_O;
  end record;

procedure Get_3(DSThree: out DS3) is
   Space: Character;            
   Char_T: Character; -- 新增变量用于接收输入的'T'/'F'字符
begin 
   Put("Mata in datamängd: ");              
   Get(DSThree.J.Y);
   Get(Space);
   Get(DSThree.J.Q);
   Get(Space);
   Get(DSThree.B.Y);
   Get(Space);
   Get(DSThree.B.Q);
   Get(Space);              
   -- 替换原直接读取布尔值的逻辑,改为读取字符后赋值
   Get(Char_T);
   if Char_T = 'T' then
      DSThree.O.T := True;
   elsif Char_T = 'F' then
      DSThree.O.T := False;
   else
      -- 可选:处理非法输入,比如输出错误提示
      Put_Line("Error: Invalid input for boolean field, expected 'T' or 'F'");
      DSThree.O.T := False; -- 非法输入时保持默认值
   end if;
   Get(Space);
   Get(DSThree.O.L);
end Get_3;

procedure Put_3(DSThree: in DS3) is
begin 
   Put("Inmatad datamängd: ");
   Put(DSThree.J.Y);
   Put(" ");
   Put(DSThree.J.Q);
   Put(" ");
   Put(DSThree.B.Y);
   Put(" ");
   Put(DSThree.B.Q);
   Put(" ");                 
   -- 替换原直接输出布尔值的逻辑,改为判断后输出文本
   if DSThree.O.T then
      Put("True");
   else
      Put("False");
   end if;
   Put(" ");
   Put(DSThree.O.L);           
end Put_3;

关键改动说明

  1. Get_3过程调整:

    • 新增Char_T字符变量,用于接收用户输入的'T'/'F'字符
    • 替换原直接读取布尔值的Get(DSThree.O.T)语句,改为读取字符后通过if分支给布尔成员T赋值
    • 可选添加非法输入处理逻辑,确保输入不符合要求时程序仍能正常运行
  2. Put_3过程调整:

    • 替换原直接输出布尔值的Put(DSThree.O.T)语句,通过if分支判断布尔成员T的值,输出对应的"True"或"False"文本
  3. 核心约束满足:
    完全保留了Sub_O记录中T: Boolean:= False;的初始化语句,未做任何修改,符合作业要求。

内容的提问来源于stack exchange,提问作者Zeptuz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:05:18