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

问题描述 投票:0回答:1

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

说明如下,

• Write a case statement with 4 alternatives that displays information on a roll of a pair of dice.
– A winning roll (7 or 11)
– A losing roll (2 or 3 or 12)
– A point roll (4 or 5 or 6 or 8 or 9 or 10)
– An invalid roll (less than 2 or greater than 12)
• Use the others option for the point roll
– In reality, I’d use the others option for invalid rolls
– The expressions Integer’First and Integer’Last yield the smallest possible (most negative) integer and largest possible integer.

我可以想出以下代码,但不知道如何处理

An invalid roll
情况, 我尝试在线搜索,阅读 docs.adacore.com,但找不到任何可以帮助我理解的内容

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

procedure Learning is
    Dice : Integer; -- the value of a roll
begin
    Put_Line ("Enter the value of your roll of the dice");
    Get (Dice);
    New_Line;
    Put("The number ");
    Put(Item => Dice, Width => 0);
    case Dice is
        when 7 | 11 =>
            Put_Line ("is a winning dice roll");
        when 2 | 3 | 12 =>
            Put_Line ("is a losing dice roll");
        when TBD =>
            Put_Line ("is not a valid dice roll");
        when others =>
            Put_Line ("establishes the point");
    end case;
    New_Line;
end Learning;

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

ada
1个回答
0
投票

由于您需要使用

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

when Integer'First .. 1 | 13 .. Integer'Last =>
   Put_Line (" is not a valid dice roll");
© www.soinside.com 2019 - 2024. All rights reserved.