我正在自学 Ada 编程语言,在我使用的教科书中有一个练习可以打印出用户输入的数字的阶乘值。我的程序编译并运行良好,我确实得到了预期的输出,但是如果我输入 13 的值,程序会崩溃并引发错误。
我不知道为什么数字 13 会这样做。IDE 是否有错误(我使用 GNAT Studio)并且我目前使用的是 Ada 2012 标准。这是我的代码:
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
-- procedure main - begins the program
procedure main is
counter : Integer := 1;
number : Integer := 0;
factorial : Integer := 1;
begin
New_Line;
while counter <= 5 loop
Put("Enter an integer: ");
Get(number);
for Count in 1 .. number loop
factorial := factorial * Count;
end loop;
Put_Line("Factorial value is: " & integer'image(factorial));
factorial …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,掷两个骰子,并使用一组计数器来显示每个总数显示的次数。我的代码编译良好并且确实运行,但由于某种原因,前两个计数器从未增加,并且似乎按数字升序打印 - 对我来说,这听起来像是我的随机数函数的问题。但我不太明白我哪里出了问题。它也永远不会迭代正确的次数,实际上大约是一半。这是我的代码。
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Numerics.Discrete_Random;
-- procedure main - begins program execution
procedure main is
dice1, dice2, diceTotal : Integer;
type Count_Array is array(1 .. 11) of Integer;
intArray : Count_Array;
-- function returnRand - produces a random number and returns it
function returnRand return Integer is
type magicNumber is new Integer range 2 .. 12;
package Rand_Number is new Ada.Numerics.Discrete_Random(magicNumber);
use Rand_Number;
theNumber : magicNumber;
g : Generator;
begin
Reset(g);
theNumber …Run Code Online (Sandbox Code Playgroud) ada ×2