开发者

Started Pascal & Need Help On Simple Program Problems

I've written some code below and I managed it to compile but it crashes as soon as enter the balls no.

Any help is greatly appreciated!

I have posted the structured English at the end of this post.

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’
O开发者_开发技巧UTPUT ‘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’


Wow, lots of Pascal today!

The error (at least from my compiler) is on line 39:

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

You have there:

Until BallsNo = 0;

But no corresponding Repeat statement to start its loop. What is that Until supposed to go with? The same thing happens with the while loop on line 17, which I think you intended to end on line 44. In both cases, they have become single-line while loops, where only the next line is part of the loop.

A while loop in Pascal with more than one statement looks like this:

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

(For clarity, { in Pascal is the comment-start character.)

Getting used to indenting your code better will help clarify what's going on, too.


Edited for your edits First, you should either make your edits more apparent, or post a new question. I didn't notice the edits until I noticed your second account added Repeat statements into your question, where they didn't exist before, and had to go hunting through the question edit history.

There's no crash, but there's no output after the first question, either. But that's because that's what you told it to do.

Trace through the logic you added. You have:

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

This will skip the entire main chunk of the program in the while loop, because Ptr is not < 1. Then, the last section is basically not run, either, where you say:

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

Because prior to the while loop, you had initialized both variables to 0, so neither of those lines print, and the program just exits (normally).

Keep trying! But think it through. You should probably post a new question next time, too.


Don't use ; before until:

...

Else PlayerTwoScore := CurrentScore

Until Ptr=2;

...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜