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

时间:2015-11-18 09:06:09

标签: pascal

我该如何制作这种模式?

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

  ********


 * ****** *


 ** **** **


 *** ** ***


 ****  ****


 ****  ****


 *** ** ***


 ** **** **


 * ****** *


  ********

这是我尝试过的。我将用户输入值设为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条件下。

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

2 个答案:

答案 0 :(得分:2)

您正在寻找的功能是

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.        

只需一个循环即可解决此问题 我留给你作为一个学习练习:-)

答案 1 :(得分:0)

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

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