gpt4 book ai didi

pascal - 如何在Pascal中使十字形图案与用户输入相对应?

转载 作者:行者123 更新时间:2023-12-02 05:45:22 25 4
gpt4 key购买 nike

如何制作这个图案?

您会看到下面有一个十字图案(打印在空间中)

  ********


* ****** *


** **** **


*** ** ***


**** ****


**** ****


*** ** ***


** **** **


* ****** *


********

这是我尝试过的。为了测试目的,我将用户输入值设置为 10。

uses crt;
var a,b,n,space1,space2 : integer;
begin
clrscr;
n:= 10;

space1:=1;
space2:=n;
for a:=1 to n do
begin
for b:=1 to n do
begin

if(b=space1) OR (b=space2-1) then
begin
write(' ');
space1:=space1+1;
space2:=space2-1;
end else
write('*');
end;

writeln;

end;
readkey;

我在打印空格字符时遇到问题。

我认为问题出在 if 条件上。

这种情况的最佳条件是什么?

最佳答案

您正在寻找的功能是

StringOfChar(c: char, i: SizeInt): AnsiString

示例:创建 5 *

  var s: string;
..
s := StringOfChar('*', 5);
..

为了获得进一步的帮助,您应该在您的工作中提供一些示例代码。

一种方法是:

uses crt;
var a,b,n,space1,space2 : integer;
s: string;
begin
clrscr;
n:= 10;

// first half of cross
for a:=0 to n div 2 -1 do begin
s := StringOfChar('*', n-2);
Insert(' ', s, a+1); // first space
Insert(' ', s, n-a); // last space
writeln(s);
end;

// second half of cross
for a:= (n div 2 -1) downto 0 do begin
s := StringOfChar('*', n-2);
Insert(' ', s, a+1); // first space
Insert(' ', s, n-a); // last space
writeln(s);
end;

writeln;
readkey;
end.

您也可以只用一个循环来解决这个问题。
我把它留给你作为学习练习:-)

关于pascal - 如何在Pascal中使十字形图案与用户输入相对应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com