如何添加 < 和 > 检查以防万一...何时?

use*_*404 3 ada

我是 Ada 编程语言新手,正在做练习,

\n

说明如下,

\n
\xe2\x80\xa2 Write a case statement with 4 alternatives that displays information on a roll of a pair of dice.\n\xe2\x80\x93 A winning roll (7 or 11)\n\xe2\x80\x93 A losing roll (2 or 3 or 12)\n\xe2\x80\x93 A point roll (4 or 5 or 6 or 8 or 9 or 10)\n\xe2\x80\x93 An invalid roll (less than 2 or greater than 12)\n\xe2\x80\xa2 Use the others option for the point roll\n\xe2\x80\x93 In reality, I\xe2\x80\x99d use the others option for invalid rolls\n\xe2\x80\x93 The expressions Integer\xe2\x80\x99First and Integer\xe2\x80\x99Last yield the smallest possible (most negative) integer and largest possible integer.\n
Run Code Online (Sandbox Code Playgroud)\n

我可以想出以下代码,但无法弄清楚如何处理An invalid roll案例,\n我尝试在线搜索,阅读 docs.adacore.com,但找不到任何可以帮助我理解的内容

\n
with Ada.Text_IO; use Ada.Text_IO;\nwith Ada.Integer_Text_IO; use Ada.Integer_Text_IO;\n\nprocedure Learning is\n    Dice : Integer; -- the value of a roll\nbegin\n    Put_Line ("Enter the value of your roll of the dice");\n    Get (Dice);\n    New_Line;\n    Put("The number ");\n    Put(Item => Dice, Width => 0);\n    case Dice is\n        when 7 | 11 =>\n            Put_Line ("is a winning dice roll");\n        when 2 | 3 | 12 =>\n            Put_Line ("is a losing dice roll");\n        when TBD =>\n            Put_Line ("is not a valid dice roll");\n        when others =>\n            Put_Line ("establishes the point");\n    end case;\n    New_Line;\nend Learning;\n
Run Code Online (Sandbox Code Playgroud)\n

我正在使用JDoodle来编译我的程序。

\n

tra*_*god 7

由于您需要使用case语句,但禁止使用子句来捕获无效的掷骰,因此请检查您可以指定离散选择的others其他方式。在可用的选项中,范围似乎很有前途,因为它允许使用问题陈述中提到的标量类型属性。

\n
when Integer\'First .. 1 | 13 .. Integer\'Last =>\n   Put_Line (" is not a valid dice roll");\n
Run Code Online (Sandbox Code Playgroud)\n

附录:作为替代方案,还可以考虑子类型指示

\n
subtype Below_2 is Integer range Integer\'First .. 1;\nsubtype Above_12 is Integer range 13 .. Integer\'Last;\n\xe2\x80\xa6\nwhen Below_2 | Above_12 =>\n   Put_Line (" is not a valid dice roll");\n
Run Code Online (Sandbox Code Playgroud)\n

  • 我还没有详细学习“范围”,但这很有效! (2认同)