gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:45:38 24 4
gpt4 key购买 nike

我刚刚了解了Ada中的Enums和Types,并决定编写一个小程序进行练习:

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是否在“工作日”子类型或“周末”子类型中?

最佳答案

你要

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中的单词!

关于types - 如何检查元素是否属于一个子类型或另一个子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9045646/

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