2011-04-17 82 views
0

我写了一些代码下面,我管理它编译,但它一旦进入球没有崩溃。开始帕斯卡和需要帮助简单的程序问题

任何帮助,非常感谢!

我在这篇文章的最后发布了结构化英语。

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

哇,今天很多帕斯卡的!

(从我的编译器至少)错误是39行:

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

你必须有:

Until BallsNo = 0; 

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

一个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,使那些都行打印的,程序只是退出(一般)。

继续尝试!但考虑一下。你下次也应该发布一个新问题。

0

之前,不要使用;到:

... 

Else PlayerTwoScore := CurrentScore 

Until Ptr=2; 

...