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

How to make cross shaped pattern corresponding with user input in Pascal?

我该如何制作这个图案?

你会看到下面有一个十字图案(印在space)

  ********


 * ****** *


 ** **** **


 *** ** ***


 ****  ****


 ****  ****


 *** ** ***


 ** **** **


 * ****** *


  ********

这是我试过的方法。为了测试目的,我将用户输入值设置为 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;

我无法打印 space 字符。

我觉得是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.        

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

您需要将 "if" 更改为以下内容:

if (a = b) or (b = N - a + 1) then
    write(' ')
else
    write('*');