我遇到For循环问题。我需要它来计算随机数量的购物狂欢和金额,但按照下面的顺序显示它们。它还需要花费大量的精力并在之前显示它。
目前,它显示的总次数为0,仅显示20个不随机的特权。
您已经赢得了3次购物狂欢
在狂欢#1上你可能会花费R100
在狂欢#2上你可以花R341
狂欢#3你可以花R451
TotalCost:= 0;
总和:= 0;
ListHead := 'Max per spree is R500.00 Max number of sprees 20';
lstNumber.Items.Add(ListHead);
SpreeWon := 'You have won ' + inttostr(Sum) + ' shopping sprees';
lstNumber.Items.Add(SpreeWon);
for Count := 0 to 20 do
begin
Sum := Random(Count);
Prize := Random(500) + 1;
ListItems := 'On spree # ' + inttostr(Sum) + ' you may spend R' + inttostr(Prize);
lstNumber.Items.Add(ListItems);
TotalCost := TotalCost + Prize;
end;
begin
Cost := 'Total prize value : R' + inttostr(TotalCost);
lstNumber.Items.Add(Cost);
end;
答案 0 :(得分:2)
您的代码没有按照您的要求进行操作。您正在显示20个sprees,因为您对其进行了硬编码以生成20个sprees,而不是随机数量的sprees。
尝试更像这样的东西:
ListHead := 'Max per spree is R500.00 Max number of sprees 20';
lstNumber.Items.Add(ListHead);
Count := Random(20) + 1;
TotalCost := 0;
SpreeWon := 'You have won ' + IntToStr(Count) + ' shopping sprees';
lstNumber.Items.Add(SpreeWon);
for I := 1 to Count do
begin
Prize := Random(500) + 1;
TotalCost := TotalCost + Prize;
ListItems := 'On spree # ' + IntToStr(I) + ' you may spend R' + IntToStr(Prize);
lstNumber.Items.Add(ListItems);
end;
Cost := 'Total prize value : R' + IntToStr(TotalCost);
lstNumber.Items.Add(Cost);