在PL/SQL中打印字母金字塔 [英] Printing an alphabet pyramid in PL/SQL
问题描述
我有一个练习来编写一个程序,该程序可以打印出像这样的字母金字塔:
I have an exercise to write a program which prints out an alphabet pyramid like this:
A
ABA
ABCBA
ABCDCBA
ABCDFDCBA
该任务还建议使用INSTR,LPAD,UPPER.
The task also suggests using INSTR, LPAD, UPPER.
我想拥有一个包含字母表中所有字母的金字塔.但是,我发现更容易将数字放在第一位,所以:
I want to have the pyramid which contains the all the letters in the alphabet. However, I find it is easier to make it in numbers first so:
IF x in 0..25 loop
dbms_output.put_line(x);
end loop;
end;
结果只是数字从0到25的直线.我不知道如何在数字之前添加空格,而最终需要使用字母来形成金字塔形状. 请不要给出完整的答案,我只需要一些建议和建议即可解决此任务.
The result is just a straight line of numbers from 0-25. I don't know how to add spaces before the numbers, which eventually needs to be letters, to form a the pyramid shape. Please don't give the full answer, I just need some suggestions and diretions to solve this task.
推荐答案
一种方法是生成金字塔的底面:
One way of doing this is to generate the base of the pyramid:
-
CHR(n)
将生成一个ASCII字符;和 - 您可以使用
||
连接字符串,因此str := str || CHR(65)
会将A
附加到字符串的末尾. -
FOR i IN [REVERSE] 1 .. repetitions LOOP
语句可以是用于遍历值.
CHR(n)
will generate an ASCII character; and- you can concatenate strings with
||
sostr := str || CHR(65)
will append anA
to the end of the string. - The
FOR i IN [REVERSE] 1 .. repetitions LOOP
statement can be used to loop through values.
如果可以生成基准,则 TRANSLATE
函数可以以一对一的方式用其他字符替换多个字符,因此:
If you can generate the base then the TRANSLATE
function can replace multiple characters with other characters in a 1-to-1 correspondence so:
-
TRANSLATE( 'ABC', 'ABC', ' A' )
输出' A'
-
TRANSLATE( 'ABC', 'ABC', ' AB' )
输出' AB'
-
TRANSLATE( 'ABC', 'ABC', 'ABC' )
输出'ABC'
TRANSLATE( 'ABC', 'ABC', ' A' )
outputs' A'
TRANSLATE( 'ABC', 'ABC', ' AB' )
outputs' AB'
TRANSLATE( 'ABC', 'ABC', 'ABC' )
outputs'ABC'
如果您生成了金字塔的底部,则您有一个字符串,其中子字符串以这些字符开头,而您只需要适当长度的子字符串并用空格填充即可.
If you have generated the base of the pyramid then you have a string where the sub-string starts with those characters and you just need sub-string of the appropriate length padded with spaces.
DBMS_OUTPUT.PUT_LINE( string )
会将字符串输出到控制台.
DBMS_OUTPUT.PUT_LINE( string )
will print a string to the console.
这篇关于在PL/SQL中打印字母金字塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!