SQL 不同的星型模式
介绍
通常,模式非常有趣。我们可以使用模式制作尽可能多的形状。让我们探索不同类型的模式。现在,我们只使用*
符号来制作形状。让我们探索可以构建不同模式的*
模式。
我正在使用的SQL Server是Oracle 10g。因此,SQL模式代码看起来与PL / SQL代码类似。
1.) 模式1
样本模式
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
模式代码
SQL> DECLARE
2 n INTEGER;
3 i INTEGER;
4 j INTEGER;
5
6 BEGIN
7
8 n:=&n
9 FOR i IN 1..n LOOP
10 FOR j IN 1..n LOOP
11 dbms_output.put('* ');
12 END LOOP;
13 DBMS_OUTPUT.NEW_LINE;
14 END LOOP;
15 END;
16 /
输入1
Enter value for n: 5
old 8: n:=&n
new 8: n:=5;
输出 2
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
PL/SQL procedure successfully completed.
输入 2
Enter value for n: 8
old 8: n:=&n
new 8: n:=5;
输出 2
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
PL/SQL procedure successfully completed.
2.) 模式 2
模式设计
*
* *
* * *
* * * *
* * * * *
模式代码
SQL> DECLARE
2 n INTEGER;
3 i INTEGER;
4 j INTEGER;
5
6 BEGIN
7
8 n:=&n
9 FOR i IN 1..n LOOP
10 FOR j IN 1..i LOOP
11 dbms_output.put('* ' );
12 END LOOP;
13 DBMS_OUTPUT.NEW_LINE;
14 END LOOP;
15 END;
16 /
输入 1
Enter value for n: 5
old 8: n:=&n
new 8: n:=5;
输出 1
*
* *
* * *
* * * *
* * * * *
PL/SQL procedure successfully completed.
输入2
Enter value for n: 10
old 8: n:=&n
new 8: n:=10;
输出 2
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
PL/SQL procedure successfully completed.
输入 3
Enter value for n: 6
old 8: n:=&n
new 8: n:=10;
输出 3
*
* *
* * *
* * * *
* * * * *
* * * * * *
PL/SQL procedure successfully completed.
3.) 模式3
模式设计
* * * * *
* * * *
* * *
* *
*
模式代码 1
SQL> DECLARE
2 n INTEGER;
3 i INTEGER;
4 j INTEGER;
5
6 BEGIN
7
8 n:=&n
9 FOR i IN 1..n LOOP
10 FOR j IN 1..n-i+1 LOOP
11 dbms_output.put('* ' );
12
13
14 END LOOP;
15 DBMS_OUTPUT.NEW_LINE;
16 END LOOP;
17 END;
18 /
输入 1
Enter value for n: 5
old 8: n:=&n
new 8: n:=5;
输出 1
* * * * *
* * * *
* * *
* *
*
PL/SQL procedure successfully completed.
输入2
Enter value for n: 6
old 8: n:=&n
new 8: n:=6;
输出 2
* * * * * *
* * * * *
* * * *
* * *
* *
*
PL/SQL procedure successfully completed.
输入3
Enter value for n: 10
old 8: n:=&n
new 8: n:=10;
输出 3
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
PL/SQL procedure successfully completed.
4.) 模式 4
模式示例
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
模式代码
SQL> DECLARE
2 n INTEGER;
3 i INTEGER:=1;
4 j INTEGER;
5 k INTEGER;
6 BEGIN
7 n:=&n
8 WHILE i=1
27 loop
28 j:=9;
29 while j>i
30 loop
31 dbms_output.put(' ');
32 j:=j-2;
33 end loop;
34
35 for k in 1..i
36 loop
37 dbms_output.put('* ');
38 end loop;
39 dbms_output.new_line;
40 i:=i-2;
41 end loop;
42 end;
43 /
输入 1
Enter value for n: 10
old 8: n:=&n
new 8: n:=10;
输出2
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
PL/SQL procedure successfully completed.
输入 2
Enter value for n: 5
old 7: n:=&n
new 7: n:=5;
输出 2
*
* * *
* * * * * * *
* * * * *
* * *
*
PL/SQL procedure successfully completed.
4.) 模式 4
模式示例
*
***
*****
*******
*********
示例代码
SQL> DECLARE
2 n number;
3 i number:=1;
4 j number;
5 k number;
6 BEGIN
7 n:=&n
8 while i
输入 1
Enter value for n: 10
old 7: n:=&n
new 7: n:=10;
输出1
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
PL/SQL procedure successfully completed.
输入2
Enter value for n: 5
old 7: n:=&n
new 7: n:=5;e
输出2
*
* * *
PL/SQL procedure successfully completed.
输入 3
Enter value for n: 20
old 7: n:=&n
new 7: n:=20;
输出3
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
PL/SQL procedure successfully completed.
5)模式5
模式示例
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
模式代码
SQL> DECLARE
2 i number;
3 j number;
4 k number;
5 BEGIN
6 i:=&i
7 while i>=1
8 LOOP
9 j:=9;
10 while j>i
11 LOOP
12 dbms_output.put(' ');
13 j:=j-2;
14 END LOOP;
15
16 for k in 1..i
17 LOOP
18 dbms_output.put('* ');
19 END LOOP;
20
21 dbms_output.new_line;
22 i:=i-2;
23 END LOOP;
24 END;
25 /
输入1
Enter value for i: 9
old 6: i:=&i
new 6: i:=9;
输出 1
* * * * * * * * * *
* * * * * * * *
* * * * *
* * *
*
输出 2
Enter value for i: 13
old 6: i:=&i
new 6: i:=9;
输出 2
* * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * *
* * *
*
6.) 模式 6
示例设计
*
**
***
****
*****
模式代码
SQL> declare
2 n number;
3 i number;
4 j number;
5 k number;
6 begin
7 n:=&n
8 for i in 1..n
9 loop
10 for j in 1..n-i
11 loop
12 dbms_output.put(' ');
13 end loop;
14
15 for k in 1..i
16 loop
17 dbms_output.put('* ');
18 end loop;
19
20 dbms_output.new_line;
21 end loop;
22 end;
23 /
输入 1
Enter value for n: 5
old 7: n:=&n
new 7: n:=5;
输出 1
*
* *
* * *
* * * *
* * * * *
PL/SQL procedure successfully completed.
输入2
Enter value for n: 9
old 7: n:=&n
new 7: n:=5;
输出 2
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
PL/SQL procedure successfully completed.