gpt4 book ai didi

string - 你如何编写一个程序,你可以在一个字符串中输入最多 5 个字符,但又少于 5 个(在 Ada 中)?

转载 作者:行者123 更新时间:2023-12-05 03:30:39 25 4
gpt4 key购买 nike

到目前为止我的代码。但是它不起作用。我希望能够写出“go”、“car”或“truck”之类的东西,只要不超过 5 个字符,然后程序就会写出那个词。我想我需要使用 Get_Line 和 Put_Line,但我不知道如何使用它们。

with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Ada_O1_1 is

I : Integer;
S : String(1..5);

begin
Put("Write a string with a maximum of 5 characters: ");
Get(S, Last =>I);
Put("You wrote the string: ");
Put(S(1..I));
end Ada_O1_1;

最佳答案

Get_Line 返回可变长度的字符串结果,Ada 要求字符串对象在实例化时具有已知大小。解决这个问题的方法是用 Get_Line 结果初始化一个 String 变量。您可以在声明 block 内执行此操作:

declare
Line : String := Get_Line;
begin
-- Do stuff here like check the length of the Line variable and
-- adjust how your code works based on that. Note that the Line
-- variable goes out of scope once you leave the block (after "end")
end;

在 block 的开始/结束部分,您可以检查返回的行的长度并验证它是否小于或等于 5,并根据该结果进行错误处理。

关于string - 你如何编写一个程序,你可以在一个字符串中输入最多 5 个字符,但又少于 5 个(在 Ada 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70779372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com