各種數字形狀列印(巢狀for迴圈的應用)
阿新 • • 發佈:2019-01-31
1、12345
12345
12345
2、
4、
5、 5
454
34543
2345432
123454321
12345
12345
12345
<span style="font-size:14px;">public class Demo01 {
public static void main(String[] args) {
for(int i = 1;i <= 5; i++){
for (int j = 1; j < 6; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
</span>
2、
11111
22222
33333
44444
55555
<span style="font-size:14px;">public class Demo02 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i);
}
System.out.println();
}
}
}</span>
3、
1
22
333
4444
55555
<span style="font-size:14px;">public class Demo03 { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } } }</span>
4、
1
12
123
1234
12345
<span style="font-size:14px;">public class Demo04 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}</span>
5、 5
454
34543
2345432
123454321
<span style="font-size:14px;">public class Homework {
public static void main(String[] args) {
for(int i=5;i>0;i--){
for(int j=i-1;j>0;j--){
System.out.print(" ");
}
for(int j=i;j<5;j++){
System.out.print(j);
}
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("==========================");
for(int i = 5;i >= 0;i--){
String txt = "";
for(int k = 1;k < (6 -i);k++){
txt = (6 - k) + txt;
}
for(int o = txt.length();o < 5;o++){
txt = " " + txt;
}
txt += new StringBuffer(txt).reverse().toString().substring(1);
System.out.println(txt);
}
}
}
</span>