无法让 Pascal 正常工作

发布于 2024-11-01 17:36:05 字数 1611 浏览 0 评论 0原文

下面的代码有什么问题?当我输入数字时它崩溃了。

我花了两个多小时试图弄清楚但仍然无法解决,我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌生 2024-11-08 17:36:05

指针:=1;
当 Ptr < 1 这样做

这是你的问题。我相信你必须用 BallsNo 替换 1。

Ptr:=1;
While Ptr < 1 Do

This is your problem. I believe you have to replace 1 with BallsNo.

白衬杉格子梦 2024-11-08 17:36:05

更改

当 Ptr < 时 1

当 Ptr > 时1

算法中存在一个有缺陷的循环。

-干杯!

Change

While Ptr < 1

to

While Ptr > 1

You have a buggy loop further down the algorithm.

-Cheers!

眼前雾蒙蒙 2024-11-08 17:36:05

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."?

最后的乘客 2024-11-08 17:36:05

请不要说“它崩溃了”。它给你某种错误消息,它是什么?

你还有一个问题,无论哪个玩家,他的分数都是零。不过,这不会导致崩溃。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文