看了博友的C# 实现刘谦春晚魔术很好,改成了delphi版的。
1 program Project1; 2 3 {$APPTYPE CONSOLE} 4 {$R *.res} 5 6 uses 7 System.SysUtils, 8 System.Classes; 9 10 var 11 list, removeElement: TStringList; 12 i, NameCount, insertIndex, directCount, sexCount: Integer; 13 tmp, element, hiddenEle1, hiddenEle2: String; 14 15 procedure Fun1(NameCount: Integer); 16 begin 17 if NameCount < list.Count then 18 begin 19 for i := 0 to NameCount - 1 do 20 begin 21 element := list[0]; // 获取第0个元素 22 list.Delete(0); // 移除第0个元素 23 list.Add(element); // 将第0个元素添加到最后 24 end; 25 26 end; 27 end; 28 29 procedure Fun2(forCount: Integer); 30 begin 31 // 被删除掉的三张牌 32 removeElement := TStringList.Create; 33 for i := 0 to forCount - 1 do 34 begin 35 removeElement.Add(list[0]); 36 list.Delete(0); 37 end; 38 Writeln(Format('被删除掉的%D张牌:%s', [forCount, removeElement.CommaText])); 39 Writeln('删除之后:' + list.CommaText); 40 41 // 把拿到的三张牌插到中间 42 Randomize; 43 insertIndex := Random(list.Count - 1) + 1; // 生成一个介于第一个和倒数第二个位置之间的随机插入位置 44 Writeln('插入位置:' + insertIndex.ToString); 45 for i := removeElement.Count - 1 downto 0 do 46 begin 47 list.Insert(insertIndex, removeElement[i]); 48 end; 49 50 end; 51 52 procedure Fun3(); 53 begin 54 // 藏起来第一张牌 55 hiddenEle1 := list.Strings[0]; 56 list.Delete(0); 57 end; 58 59 procedure Fun4(Count: Integer); 60 begin 61 for i := 0 to Count - 1 do 62 begin 63 list.Delete(0); 64 end 65 end; 66 67 procedure Fun5(); 68 begin 69 for i := 0 to 6 do 70 begin 71 element := list[0]; 72 list.Delete(0); 73 list.Add(element); 74 end; 75 end; 76 77 function Fun6(sexCount: Integer): string; 78 var 79 forCount: Integer; 80 begin 81 forCount := 0; 82 if sexCount = 1 then 83 forCount := 5 // 男生要5次 84 else 85 forCount := 4; 86 for i := 0 to forCount - 1 do 87 begin 88 // 把第一张牌放到数组最后面 (好运留下来) 89 element := list[0]; 90 list.Delete(0); 91 list.Add(element); 92 93 // 烦恼丢出去 94 list.Delete(0); 95 end; 96 result := list[0]; 97 end; 98 99 begin 100 try 101 try 102 list := TStringList.Create; 103 list.CommaText := 'A,B,C,D,A,B,C,D'; 104 Writeln('初始牌组:' + list.CommaText); 105 Writeln('请输入你的名字有几个字:'); 106 ReadLn(tmp); 107 NameCount := tmp.ToInteger; 108 109 Fun1(NameCount); 110 Writeln('变化之后:' + list.CommaText); 111 112 Fun2(3); 113 Writeln('插入三张牌后:' + list.CommaText); 114 Fun3(); 115 Writeln('藏起来第一张牌后:' + list.CommaText); 116 117 Writeln('北方-1 南方-2 不南不北-3,请输入对应数字:'); 118 ReadLn(tmp); 119 directCount := tmp.ToInteger; 120 Fun2(directCount); 121 Writeln('南北方人后:' + list.CommaText); 122 123 Writeln('男-1 女-2,请输入对应数字:'); 124 ReadLn(tmp); 125 sexCount := tmp.ToInteger; 126 Fun4(sexCount); 127 Writeln('男女生后:' + list.CommaText); 128 129 Fun5(); 130 131 Writeln('见证奇迹后:' + list.CommaText); 132 133 Writeln('开始好运留下了 烦恼丢出去'); 134 hiddenEle2 := Fun6(sexCount); 135 Writeln(Format('牌1:%s 牌2:%s', [hiddenEle1, hiddenEle2])); 136 137 ReadLn; 138 except 139 on E: Exception do 140 Writeln(E.ClassName, ': ', E.Message); 141 end; 142 143 finally 144 removeElement.Free; 145 list.Free; 146 147 end; 148 149 end.
标签:begin,end,CommaText,刘谦,Delphi,list,Writeln,春晚,forCount From: https://www.cnblogs.com/homejun/p/18018009