写 Delphi 程序 输入一个整数n,计算1~n之间的平方和,立方和,偶数和

1个回答

  • procedure TForm1.Button1Click(Sender:TObject);

    var

    v_Index,N:Integer;

    SquareSum,CubeSum,EvenSum:Integer;

    ibegin ,iEnd :integer;

    begin

    N := StrToIntDef(Edit1.Text,0);

    SquareSum := 0;

    CubeSum :=0;

    EvenSum := 0;

    if (n < 1)

    begin

    iBegin := N;

    iEnd := 1;

    end

    else

    begin

    iBegin := 1;

    iEnd := N;

    end;

    for v_Index := iBegin to iEnd do

    begin

    SquareSum := SquareSum + v_Index * v_Index;

    CubeSum := CubeSum + v_Index * v_Index * v_Index;

    if v_Index mod 2 = 0 then

    begin

    EvenSum := EvenSum + v_Index;

    end;

    end;

    ShowMessage('1~' + IntToStr(N) + '的平方和:' + IntToStr(SquareSum) + ','+#10#13

    + '立方和:' + IntToStr(CubeSum) + ',' + #10#13 +

    '偶数和:' + IntToStr(EvenSum));

    end;