如何检查一个元素是否属于一种子类型或另一种子类型?

发布于 2024-12-29 15:18:33 字数 1308 浏览 1 评论 0原文

我刚刚了解了 Ada 中的枚举和类型,并决定编写一个小程序来练习:

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

procedure Day is 

    type Day_Of_The_Week is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    subtype Weekday is Day_Of_The_Week range Monday..Friday;

    subtype Weekend is Day_Of_The_Week range Saturday..Sunday;

        function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
        begin
            if(--?--)
        end is_Weekday;

    selected_day_value  :   Integer;
    selected_day                :   Day_Of_The_Week;

begin
    Put_Line("Enter the number co-responding to the desired day of the week:");
    Put_Line("0 - Monday");
    Put_Line("1 - Tuesday");
    Put_Line("2 - Wednesday");
    Put_Line("3 - Thursday");
    Put_Line("4 - Friday");
    Put_Line("5 - Saturday");
    Put_Line("6 - Sunday");
    Get(selected_day_value);
    selected_day = Day_Of_The_Week'pos(selected_day_value);

    if( is_Weekday(selected_day))
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );
    else
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );

end Day;

我在 if 语句方面遇到了麻烦。如何检查 dayOfTheWeek 是否属于工作日子类型或周末子类型?

I just learnt about Enums and Types in Ada and decided to write a small program to practice:

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

procedure Day is 

    type Day_Of_The_Week is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    subtype Weekday is Day_Of_The_Week range Monday..Friday;

    subtype Weekend is Day_Of_The_Week range Saturday..Sunday;

        function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
        begin
            if(--?--)
        end is_Weekday;

    selected_day_value  :   Integer;
    selected_day                :   Day_Of_The_Week;

begin
    Put_Line("Enter the number co-responding to the desired day of the week:");
    Put_Line("0 - Monday");
    Put_Line("1 - Tuesday");
    Put_Line("2 - Wednesday");
    Put_Line("3 - Thursday");
    Put_Line("4 - Friday");
    Put_Line("5 - Saturday");
    Put_Line("6 - Sunday");
    Get(selected_day_value);
    selected_day = Day_Of_The_Week'pos(selected_day_value);

    if( is_Weekday(selected_day))
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );
    else
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );

end Day;

I'm having trouble with the if statement. How can I check whether or not dayOfTheWeek is in the Weekday subtype or the weekend subtype?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2025-01-05 15:18:33

另外,您希望

function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
begin
    return dayoFTheWeek in Weekday;
end is_Weekday;

输入 'Val 而不是 'Pos

selected_day := Day_Of_The_Week'val(selected_day_value);

,并且您可以查看第二个 Put_Line 中的单词!

You want

function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
begin
    return dayoFTheWeek in Weekday;
end is_Weekday;

Also, you want ’Val not ’Pos in

selected_day := Day_Of_The_Week'val(selected_day_value);

and you might take a look at the words in the second Put_Line!

极度宠爱 2025-01-05 15:18:33

您不需要一个函数来检查这一点。在这种情况下,函数只会掩盖发生的事情:

if Selected_Day in Weekday then
  do stuff..
else
  do other stuff...
end if;

You don't need a function to check for this. In this case a function only obscures what happens:

if Selected_Day in Weekday then
  do stuff..
else
  do other stuff...
end if;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文