delphi编写程序求1+2+3+…+100之间的偶数和(不包含50)

如题所述

第1个回答  2022-12-16
以下代码是在 Delphi 窗体应用程序中进行编写:
var
i, sum: Integer;
begin
sum := 0;
for i := 1 to 100 do
begin
if (i = 50) then
Continue;
if (i mod 2 = 0) then
sum := sum + i;
end;
ShowMessage(IntToStr(sum));
end;
上面的代码会在循环内部检查每个数字是否为偶数,如果是就将其加入到sum变量中。如果当前数字为50,就使用Continue语句跳过本次循环。最后,使用ShowMessage函数显示结果。
相似回答