如何检查一个元素是否属于一种子类型或另一种子类型?
我刚刚了解了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,您希望
输入
'Val
而不是'Pos
,并且您可以查看第二个
Put_Line
中的单词!You want
Also, you want
’Val
not’Pos
inand you might take a look at the words in the second
Put_Line
!您不需要一个函数来检查这一点。在这种情况下,函数只会掩盖发生的事情:
You don't need a function to check for this. In this case a function only obscures what happens: