Delphi 2010的循环计数 [英] Delphi 2010 for loop count

查看:83
本文介绍了Delphi 2010的循环计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对For循环有问题。我需要它来计算随机的购物金额和数量,但按如下顺序显示它们。还需要先取一定数量的鱼露并显示出来。



当前它显示的总sprees为0,并且仅显示20个非随机的sprees。



您已经赢了总共3种购物狂



在狂欢1号上,您可以消费R100



在狂欢2号上,您可以在大礼包#3上花费R341



您可以花费R451



TotalCost:= 0;



总和:= 0;

  ListHead:='每个大礼包的最大值为R500.00壁饰最大数量20'; 
lstNumber.Items.Add(ListHead);

SpreeWon:=‘您赢了’+ intstr(Sum)+’购物狂’;
lstNumber.Items.Add(SpreeWon);

的Count:= 0到20做
开始
Sum:= Random(Count);
奖:=随机(500)+ 1;
ListItems:=‘大礼包#’+ inttostr(Sum)+’您可以花费R’+ inttostr(Prize);
lstNumber.Items.Add(ListItems);
TotalCost:= TotalCost +奖金;
结尾;
开始
费用:=总奖金额:R + inttostr(TotalCost);
lstNumber.Items.Add(Cost);
结尾;


解决方案

您的代码没有满足您的要求。您正在显示20个sprees,因为您对其进行了硬编码以生成20个sprees,而不是随机数量的sprees。 b
$ b

  ListHead:='每次狂欢的最大金额为R500.00最多可容纳20个幕布;' 
lstNumber.Items.Add(ListHead);

计数:= Random(20)+ 1;
TotalCost:= 0;

SpreeWon:=‘您赢了’+ IntToStr(Count)+’购物狂’;
lstNumber.Items.Add(SpreeWon);
I的

:= 1计数
开始
奖金:= Random(500)+ 1;
TotalCost:= TotalCost +奖金;
ListItems:=‘大礼包#’+ IntToStr(I)+’您可以花费R’+ IntToStr(Prize);
lstNumber.Items.Add(ListItems);
结尾;

费用:=总奖金价值:R + IntToStr(TotalCost);
lstNumber.Items.Add(Cost);


I have a problem with a For Loop. I need it to count a random amount of shopping sprees and amounts but display them in order like below. It also needs to take the amount of sprees and display it before.

Currently it displays total sprees as 0 and only displays 20 sprees which is not random.

You have won a total of 3 shopping sprees

On spree #1 you may spend R100

On spree #2 you may spend R341

On spree #3 you may spend R451

TotalCost := 0;

Sum := 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;

解决方案

Your code is not doing what your requirement asks for. You are displaying 20 sprees because you hard-coded it to generate 20 sprees, not a random number of sprees.

Try something more like this instead:

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);

这篇关于Delphi 2010的循环计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆