启动Pascal&需要有关简单程序问题的帮助

时间:2011-04-17 12:00:20

标签: pascal

我已经在下面编写了一些代码并且我将其设置为编译但是一旦输入球就会崩溃。

非常感谢任何帮助!

我在本帖末尾发布了结构化英语。

Program Game;

var
PlayerOneScore: Integer;
PlayerTwoScore: Integer;
BallsNo: Integer;
CurrentScore: Integer;
Ptr: Integer;
Result:Integer;

Begin
PlayerOneScore:= 0;
PlayerTwoScore:= 0;
Writeln('How many balls do you wish to face?');
Readln(BallsNo);
Ptr:=1;
While Ptr < 1 Do
Begin
Repeat
Ptr:=Ptr+1;
CurrentScore:=0;
Writeln ('Player turn');
Writeln ('Please roll the bowling die');
Writeln ('Enter 1 if result is a 1');
Writeln ('Enter 2 if result is a 2');
Writeln ('Enter 3 if result is a 4');
Writeln ('Enter 4 if result is a 6');
Writeln ('Enter 5 if result is a 0');
        While BallsNo >0 Do
        Begin
        Repeat
        BallsNo:=BallsNo-1;
        Writeln('This is',BallsNo);
        Readln(Result);
        If Result = 1 Then
           CurrentScore:= CurrentScore+1
           Else If Result = 2 THEN
           CurrentScore:= CurrentScore+2
           Else If Result  = 3 THEN
           CurrentScore:= CurrentScore+4
           Else If Result=4 THEN
           CurrentScore := CurrentScore+6
        Until BallsNo = 0;
        End;
If Ptr = 1 THEN
PlayerOneScore := CurrentScore
Else PlayerTwoScore := CurrentScore;
Until Ptr=2;
End;
If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');
End.

==================

PlayerOneScore <- 0
PlayerTwoScore <- 0
OUTPUT ‘How many balls do you wish to face?’
INPUT BallsNo
FOR EachPlayer <- 1 TO 2 DO
    CurrentScore <- 0
    OUTPUT ‘Player’ + EachPlayer + ‘to go’
OUTPUT ‘Please roll the bowling die’
    OUTPUT ‘Enter 1 if result is a 1’
OUTPUT ‘Enter 2 if result is a 2’
OUTPUT ‘Enter 3 if result is a 4’
OUTPUT ‘Enter 4 if result is a 6’
OUTPUT ‘Enter 5 if result is a 0’
    FOR EachBall <- 1 TO BallsNo DO
        OUTPUT ‘Ball number: ‘ + EachBall
INPUT BowlResult
IF BowlResult = 1 THEN
    CurrentScore <- CurrentScore + 1
ELSE IF BowlResult = 2 THEN
    CurrentScore <- CurrentScore + 2
ELSE IF BowlResult = 3 THEN
    CurrentScore <- CurrentScore + 4
ELSE IF BowlReuslt = 4 THEN
    CurrentScore <- CurrentScore + 6
END IF
END FOR
IF EachPlayer = 1 THEN
    PlayerOneScore <- CurrentScore
ELSE
    PlayerTwoScore <- CurrentScore
END FOR
IF PlayerOneScore > PlayerTwoScore THEN
    OUTPUT ‘Player One Wins’
ELSE IF PlayerTwoScore > PlayerOneScore THEN
    OUTPUT ‘Player Two Wins’
ELSE
    OUTPUT ‘Draw’

2 个答案:

答案 0 :(得分:2)

哇,今天很多帕斯卡尔!

错误(至少来自我的编译器)在第39行:

foo.pas(39,9) Fatal: Syntax error, ";" expected but "UNTIL" found

你在那里:

Until BallsNo = 0;

但没有相应的Repeat语句来启动它的循环。什么是Until应该配合?同样的事情发生在第17行的while循环,我认为你打算在第44行结束。在这两种情况下,它们都变成了单行while循环,其中只有下一行是循环的一部分

Pascal中带有多个语句的while循环如下所示:

while CONDITION do
begin
    { statements... }
end;

(为清楚起见,Pascal中的{是注释开始字符。)

习惯于更好地缩进代码也有助于澄清正在发生的事情。


编辑您的修改首先,您应该使编辑更加明显,或者发布新问题。我没有注意到这些修改,直到我发现您的第二个帐户在您的问题中添加了Repeat个语句,之前它们不存在,并且必须查看问题编辑历史记录。

没有崩溃,但在第一个问题之后也没有输出。但那是因为那是你告诉它的事情。

追踪您添加的逻辑。你有:

Ptr:=1;
While Ptr < 1 Do
{ .... }
End;

这会跳过while循环中程序的整个主要部分,因为Ptr不是< 1。然后,最后一节基本上没有运行,你说:

If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');

因为在while循环之前,您已将两个变量初始化为0,因此这些行都不打印,并且程序刚退出(通常)。

继续尝试!但想一想。您下次也应该发布一个新问题。

答案 1 :(得分:0)

之前请不要使用;

...

Else PlayerTwoScore := CurrentScore

Until Ptr=2;

...