如何检查元素是否属于一个子类型? [英] How can you check if an element belongs to one subtype or another?

查看:131
本文介绍了如何检查元素是否属于一个子类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

我在使用if语句时遇到问题。如何检查dayOfTheWeek是否在Weekday子类型或周末子类型?

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?

推荐答案

b
$ b

You want

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

此外,您还需要'Val code>

Also, you want ’Val not ’Pos in

selected_day := Day_Of_The_Week'val(selected_day_value);

,你可以看看第二个 Put_Line

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

这篇关于如何检查元素是否属于一个子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆