怪事,大家来看看这个问题

yypp 2001-11-08 03:20:20
var s1,temp,s2:String;
i,j,count:integer;
begin
s1:='123456789';
count:=Length(s1);

for i:=0 to count do
begin
randomize;
j := random(Length(s1));
temp:=s1[j];
Delete(s1,j,1);
s2:=s2+temp;
end;
edit1.Text:=s2;
end;

为什么最后得到的字符串长度不一?
有时候一个字符也没有?
...全文
97 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BBfox 2001-11-08
  • 打赏
  • 举报
回复
s1[]是从1到9,所以j := random(Length(s1))+1
tikkypeng 2001-11-08
  • 打赏
  • 举报
回复
L1: j := random(Length(s1));
if (j=0) and (Length(s1)<>1) then goto L1;

random指定了最大值~~可是可能取零~~所以可能出现空的~~所以增加判断~~
如果是0~则从新取值~~
当剩余最后一个字符时~~random(Length(s1))只能取0~~进入死循环~~
所以判断条件为(j=0) and (Length(s1)<>1)~~
BBfox 2001-11-08
  • 打赏
  • 举报
回复
var s1,temp,s2:String;
i,j,count:integer;
begin
s1:='123456789';
count:=Length(s1);

for i:=1 to count do
begin
randomize;
j := random(Length(s1))+1;
temp:=s1[j];
Delete(s1,j,1);
s2:=s2+temp;
end;
edit1.Text:=s2;
end;
tikkypeng 2001-11-08
  • 打赏
  • 举报
回复
var
s1,temp,s2:String;
i,j,count:integer;
Label L1;
begin
s1:='123456789';
count:=Length(s1);
randomize;
for i:=0 to Count-1 do
begin
L1: j := random(Length(s1));
if (j=0) and (Length(s1)<>1) then goto L1;
temp:=s1[j];
Delete(s1,j,1);
s2:=s2+temp;
end;
Edit1.Text := s2;
end;
yypp 2001-11-08
  • 打赏
  • 举报
回复
改成count-1还是一样,最后显示在Edit1里的字符串有时只有3个字符,有时候有8个,有时候是空白
lovewangj 2001-11-08
  • 打赏
  • 举报
回复
countL=Length(s1)-1;
应该这样,因为循环时应该从0到count-1的。

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧