如何在ID号中添加破折号?

问题描述

初学者问题!。我想请用户写下他/她的生日(例如:19980231),然后像这样打印出来:1998-02-31。在Ada中的数字之间添加破折号看起来很简单,但我不知道。有什么建议?这就是我已经走了多远了

with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;              use Ada.Float_Text_IO;

  procedure Dugga is
  N,M,Z,Y,X:Integer;
begin
Put("Write ur date of birth: ");
Get(X);
Y:= X/10000;
Put(Y); Put("-");
Z:= X - (Y*10000);
M:= Z/100;
if 9 < M then
Put(M,width=>0); Put("-");
elsif 9 >= M then
Put("0"); Put(M,width=>0);  Put("-");
end if;
Put(M,width=>0); Put("-");
N:= Z - (M*100);
Put(N,width=>0);
end Dugga;

解决方法

以下程序将输入作为字符串读取,检查是否输入了正确的字符数。

with Ada.Text_IO; use Ada.Text_IO;

procedure Birthday is
   Raw    : String (1 .. 8);
   Length : Natural := 0;
begin
   loop
      Put ("Enter a date in the format YYYYMMDD: ");
      Get_Line (Raw,Length);
      exit when Length = 8;
      Put_Line ("Wrong input format. Try again.");
   end loop;
   Put_Line (Raw (1 .. 4) & "-" & Raw (5 .. 6) & "-" & Raw (7 .. 8));
end Birthday;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...