How to set the initial sumComp value, sumHum to zero, now each time you press the button displays large numbers (<16000)

procedure TForm1.Button1Click(Sender: TObject); var hDice, cDice:shortint; sumComp, sumHum: integer; begin Randomize; hDice :=random(6); cDice :=random(6); Label1.Caption := IntToStr(hDice+1); Label2.Caption := IntToStr(cDice+1); ... sumComp := sumComp+cDice; sumHum := sumHum+hDice; Label6.Caption:=IntToStr(sumComp); 
  • one
    0 they tried to assign? - Anton Shchyrov
  • Each time you press the sumComp button, it will assign 0 to it, and I need only when the program starts - NickGrom
  • 3
    Well, make the variables sumComp , sumHum class fields - Anton Shchyrov

1 answer 1

Project - Options - Compiler - Assignable typed constants - check.

 procedure TForm1.Button1Click(Sender: TObject); var hDice, cDice: shortint; const sumComp: integer = 0; sumHum: integer = 0; begin ... 
  • After that, it does not change itself (sumComp + cDice). You need to assign 0 when starting the program and that's it. There is an idea for Form1.Create, but I don’t have any idea how to do it - NickGrom
  • @NickGrom You copied the code from the answer to yourself? So what? - Igor
  • sumComp: = 1 + sumComp + cDice; gives an error with a constant stating that it cannot be changed - NickGrom
  • @NickGrom Please pay attention to the first line of my answer. - Igor
  • Thanks It works. Do not explain why it is? - NickGrom