我需要 SQL*Plus 的 PL/SQL 代码 我得到:
1
2 2
3 3 3
4 4 4 4
输出应该是:
1
2 2
3 3 3
4 4 4 4
代码:
将服务器输出大小设置为 1000000
声明
i NUMBER;
j NUMBER;
total_lines NUMBER := 4; -- Number of lines for the pyramid
leading_ spaces NUMBER;
line_output VARCHAR2(100); -- Variable to hold each line's output
开始
-- Loop through each line to create the pyramid
FOR i IN 1..total_lines LOOP
-- Calculate leading spaces for centering the line
leading_spaces := total_lines - i;
-- Initialize the line output with leading spaces
line_output := LPAD(' ', leading_spaces);
-- Append numbers to the line output
FOR j IN 1..i LOOP
line_output := line_output || i || ' ';
END LOOP;
-- Print the line output
DBMS_OUTPUT.PUT_LINE(line_output);
END LOOP;
结束;
/
代码没有给出正确的输出,但是:
1
2 2
3 3 3
4 4 4 4
PL/SQL 过程成功完成。
SQL> 而不是 1
2 2
3 3 3
4 4 4 4
应该是:
1
2 2
3 3 3
4 4 4 4
但之间没有空格 任何帮助,
谢谢,
法布里斯
它与工具无法正确显示您想要的内容有关。 这是 SQL*Plus;我使用的是
non-breakable space (chr(160)
) 而不是 ordinary 空格字符,结果是
SQL> SET SERVEROUTPUT ON SIZE 1000000
SQL> DECLARE
2 i NUMBER;
3 j NUMBER;
4 total_lines NUMBER := 4; -- Number of lines for the pyramid
5 leading_spaces NUMBER;
6 line_output VARCHAR2 (100); -- Variable to hold each line's output
7 BEGIN
8 -- Loop through each line to create the pyramid
9 FOR i IN 1 .. total_lines
10 LOOP
11 -- Calculate leading spaces for centering the line
12 leading_spaces := total_lines - i;
13
14 -- Initialize the line output with leading spaces
15 line_output := LPAD (chr(160), leading_spaces, chr(160));
16
17 -- Append numbers to the line output
18 FOR j IN 1 .. i
19 LOOP
20 line_output := line_output || i || chr(160);
21 END LOOP;
22
23 -- Print the line output
24 DBMS_OUTPUT.PUT_LINE (line_output);
25 END LOOP;
26 END;
27 /
1
2 2
3 3 3
4 4 4 4
PL/SQL procedure successfully completed.
SQL>