无法让 Pascal 正常工作
下面的代码有什么问题?当我输入数字时它崩溃了。
我花了两个多小时试图弄清楚但仍然无法解决,我正在学习 pascal,所以请耐心等待。
Program Game;
var
PlayerOneScore: Integer;
PlayerTwoScore: Integer;
BallsNo: Integer;
CurrentScore: Integer;
Ptr: Integer;
Result: Integer;
Begin
CurrentScore := 0;
PlayerOneScore:= 0;
PlayerTwoScore:= 0;
Writeln('How many balls do you wish to face?');
Readln(BallsNo);
Ptr:=BallsNo;
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
Writeln('This is',BallsNo);
Readln(Result);
BallsNo:=BallsNo-1;
Until BallsNo = 0;
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
End;
Until Ptr=2;
End;
If Ptr = 1 Then
PlayerOneScore := CurrentScore
Else
PlayerTwoScore := CurrentScore;
If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');
If PlayerOneScore = PlayerTwoScore Then
Writeln('Tie');
End.
What is wrong with the following code? It crashes when I enter a number.
I've spent over 2 hours trying to figure out and still can't, I learning pascal so please bear with me.
Program Game;
var
PlayerOneScore: Integer;
PlayerTwoScore: Integer;
BallsNo: Integer;
CurrentScore: Integer;
Ptr: Integer;
Result: Integer;
Begin
CurrentScore := 0;
PlayerOneScore:= 0;
PlayerTwoScore:= 0;
Writeln('How many balls do you wish to face?');
Readln(BallsNo);
Ptr:=BallsNo;
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
Writeln('This is',BallsNo);
Readln(Result);
BallsNo:=BallsNo-1;
Until BallsNo = 0;
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
End;
Until Ptr=2;
End;
If Ptr = 1 Then
PlayerOneScore := CurrentScore
Else
PlayerTwoScore := CurrentScore;
If PlayerOneScore > PlayerTwoScore Then
Writeln('Player One Wins');
If PlayerTwoScore > PlayerOneScore Then
Writeln('Player Two Wins');
If PlayerOneScore = PlayerTwoScore Then
Writeln('Tie');
End.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
指针:=1;
当 Ptr < 1 这样做
这是你的问题。我相信你必须用 BallsNo 替换 1。
Ptr:=1;
While Ptr < 1 Do
This is your problem. I believe you have to replace 1 with BallsNo.
更改
当 Ptr < 时 1
到
当 Ptr > 时1
算法中存在一个有缺陷的循环。
-干杯!
Change
While Ptr < 1
to
While Ptr > 1
You have a buggy loop further down the algorithm.
-Cheers!
CurrentScore 并不总是被初始化。
尝试插入“CurrentScore := 0;”在第一个“开始”之后。
另外,你怎么知道它崩溃了?
也许它只是不打印任何东西。
如果在“End.”之前添加“If PlayerOneScore = PlayerTwoScore then Writeln('Tie')”会发生什么?
CurrentScore isn't always initialized.
Try inserting "CurrentScore := 0;" after the first "Begin".
Also, how do you know it's crashing?
Maybe it's just not printing anything.
What happens if you add "If PlayerOneScore = PlayerTwoScore Then Writeln('Tie')", just before the "End."?
请不要说“它崩溃了”。它给你某种错误消息,它是什么?
你还有一个问题,无论哪个玩家,他的分数都是零。不过,这不会导致崩溃。
Please don't say "it crashes". It's giving you some sort of error message, what is it?
You've also got a problem that no matter what player one's score is zero. That won't cause a crash, though.