问题描述
|
我正在玩comp 1的预发布材料,但是我似乎无法在使用delphi进行Pascal编译中正确理解这一点。
此问题涉及函数RollBowlDie。
此功能不验证输入值。
你要做的...
制作骨架程序的副本。向函数RollBowlDie添加其他语句,以确保在允许游戏继续之前,输入BowlDieResult的范围为1到6,否则将输出错误消息。
向函数RollBowlDie添加更多语句,以便它还检查输入
BowlDieResult如果数据类型错误,则不会导致程序崩溃,而是在检测到此错误时输出错误消息。
测试修改后的功能,以表明它不允许输入超出范围。
测试修改后的函数,以表明如果添加了错误的数据类型,它不会崩溃。
这是RollBowlDie函数。
Function RollBowlDie(VirtualDiceGame : Boolean) : Integer;
Var
BowlDieResult : Integer;
Begin
If VirtualDiceGame
Then BowlDieResult := Random(6) + 1
Else
Begin
Writeln(\'Please roll the bowling die and then enter your result.\');
Writeln;
Writeln(\'Enter 1 if the result is a 1\');
Writeln(\'Enter 2 if the result is a 2\');
Writeln(\'Enter 3 if the result is a 4\');
Writeln(\'Enter 4 if the result is a 6\');
Writeln(\'Enter 5 if the result is a 0\');
Writeln(\'Enter 6 if the result is OUT\');
Writeln;
Write(\'Result: \');
Readln(BowlDieResult);
Writeln;
End;
RollBowlDie := BowlDieResult;
End;
我曾尝试将其放入,但未出现错误消息,并且尝试输入字母时程序崩溃。
Function RollBowlDie(VirtualDiceGame : Boolean) : Integer;
Var
BowlDieResult : Integer;
Begin
If VirtualDiceGame
Then BowlDieResult := Random(6) + 1
Else
Begin
Repeat
Writeln(\'Please roll the bowling die and then enter your result.\');
Writeln;
Writeln(\'Enter 1 if the result is a 1\');
Writeln(\'Enter 2 if the result is a 2\');
Writeln(\'Enter 3 if the result is a 4\');
Writeln(\'Enter 4 if the result is a 6\');
Writeln(\'Enter 5 if the result is a 0\');
Writeln(\'Enter 6 if the result is OUT\');
Writeln;
Write(\'Result: \');
Try
Readln(BowlDieResult)
Except
Writeln(\'Not a valid number\')
End;
Writeln;
Until (BowlDieResult >= 1) and (BowlDieResult <= 6);
End;
RollBowlDie := BowlDieResult;
End;
我不确定如何解决该问题,我们将不胜感激!
解决方法
可能您需要读取一个字符串或一个char型变量而不是一个整数,然后以一种受控方式将字符串/ char转换为整数。
,当然,如果您输入字母,它会崩溃...您是专门要求
读取整数,而字母不是整数。
在对这个问题发表更多评论之前,为什么不说:
writeln (\'Please enter the number rolled,or 0 if it is an OUT: \');
而不是有6个writelns?另外,如果滚动了3或5(只
为值1、2、4、6和0给出(有点奇怪)方向...离开
输出3和5。请记住,您无法从\“ BowlDieResult:= Random(6)+ 1 \”中获得0。
如果您要循环播放直到您知道之前,您打算如何让用户指示“停止”
一个介于1到6之间的值?
回到“我如何阅读一封信”问题...
使用\“ char \”类型的变量(或\“ char \的压缩数组”)读取任意文本...
然后,将读取的字符与\'1 \',\'2 \',...,\'6 \'或(例如)\'Q \'(用于退出)进行比较。
例如。:
var
answer : char;
attempts : integer; {prevent infinite loop}
done : boolean;
attempts := 0;
saw_quit := false;
done := false; {loop until we get a good number or a QUIT command}
{Or until 9 attempts have been made to enter a #. }
while not done do
begin
writeln (\'Please enter a number (1..6) or Q to quit: \');
readln (answer);
if answer in [\'1\'..\'6\'] then
begin {we got a number in range 1..6...}
BowlDieResult := ord (answer) - ord (\'0\'); {convert char to int}
done := true;
end
else if answer in [\'Q\',\'q\'] then {upper or lower case :) }
begin
saw_quit := true;
done := true;
end
else
begin
writeln (\'Sorry,that is not a number from 1 to 6 or a \"Q\"!\');
attempts := attempts + 1;
if attempts > 9 then
begin
writeln (\'Sorry,too many mistakes ... assuming QUIT\');
saw_quit := true;
done := true;
end;
end;
end; {while not done}
注意:以上未经过编译测试...我从不使用Pascal的内置I / O,
由于性能和可靠性的原因,所以我对此感到生疏。
注意:请参阅http://www.allegro.com/papers/htpp.html
对于Pascal编程的一些哲学。
斯坦