开始帕斯卡和需要有关简单程序问题的帮助
我在下面写了一些代码,我成功地编译了它,但是一旦输入球号,它就会崩溃。
非常感谢任何帮助!
我在这篇文章的末尾发布了结构化英语。
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’
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’
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’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇,今天有很多帕斯卡!
错误(至少来自我的编译器)位于第 39 行:
你有:
但没有相应的
Repeat
语句来启动其循环。Until
应该搭配什么?第 17 行的 while 循环也发生了同样的情况,我认为您打算在第 44 行结束。在这两种情况下,它们都变成了单行 while 循环,其中只有下一行是一部分循环的。Pascal 中包含多个语句的
while
循环如下所示:(为清楚起见,Pascal 中的
{
是注释开始字符。)习惯更好地缩进代码也将有助于澄清正在发生的事情。
针对您的编辑进行编辑首先,您应该使您的编辑更加明显,或者发布一个新问题。我没有注意到这些编辑,直到我注意到您的第二个帐户在您的问题中添加了
Repeat
语句,而这些语句之前并不存在,并且必须查找问题编辑历史记录。没有崩溃,但在第一个问题之后也没有输出。但那是因为这就是你告诉它要做的。
跟踪您添加的逻辑。您有:
这将在
while
循环中跳过程序的整个主要块,因为Ptr
不是1.
.然后,最后一部分基本上也不会运行,您说:因为在
while
循环之前,您已将两个变量初始化为0
,所以这两行都不是print,程序就退出(正常情况下)。继续努力!但请仔细考虑一下。下次您或许也应该提出一个新问题。
Wow, lots of Pascal today!
The error (at least from my compiler) is on line 39:
You have there:
But no corresponding
Repeat
statement to start its loop. What is thatUntil
supposed to go with? The same thing happens with thewhile
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:(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:
This will skip the entire main chunk of the program in the
while
loop, becausePtr
is not< 1
. Then, the last section is basically not run, either, where you say:Because prior to the
while
loop, you had initialized both variables to0
, 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: