>数字模式(1to9):
package b1;
public class npattern {
public static void main(string[] args) {
// npattern1();
// npattern2();
// npattern3();
// npatern4();
// npattern5();
// npattern6();
// npattern7();
// npattern8();
// npattern9();
npattern10();
}
private static void npattern10() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (col == 3 || col == 9 || col == 1)
system.out.print("* ");
else if (row == 1 && col >= 3 || row == 9 && col >= 3)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * * * * * * *
private static void npattern9() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 9 || row == 1 || col == 9)
system.out.print("* ");
else if (row == 5 || col == 1 && row <= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
* *
* *
* *
* * * * * * * * *
*
*
*
* * * * * * * * *
private static void npattern8() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 5 || row == 1 || row == 9)
system.out.print("* ");
else if (col == 1 || col == 9)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
* *
* *
* *
* * * * * * * * *
* *
* *
* *
* * * * * * * * *
private static void npattern7() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 1 || row + col == 10)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
*
*
*
*
*
*
*
*
private static void npattern6() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 5 || row == 1 || row == 9)
system.out.print("* ");
else if (col == 1 || col == 9 && row >= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
*
*
*
* * * * * * * * *
* *
* *
* *
* * * * * * * * *
private static void npattern5() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 5 || row == 1 || row == 9)
system.out.print("* ");
else if (col == 1 && row <= 5 || col == 9 && row >= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
*
*
*
* * * * * * * * *
*
*
*
* * * * * * * * *
private static void npatern4() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 5 || col == 5)
system.out.print("* ");
else if (row + col == 6 && row <= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
*
* *
* *
* *
* * * * * * * * *
*
*
*
*
private static void npattern3() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 9 || row == 5 || row == 1)
system.out.print("* ");
else if (col == 9 && row <= 5 || col == 9 && row >= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
*
*
*
* * * * * * * * *
*
*
*
* * * * * * * * *
private static void npattern2() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 9 || row == 5 || row == 1)
system.out.print("* ");
else if (col == 1 && row >= 5 || col == 9 && row <= 5)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
output:
* * * * * * * * *
*
*
*
* * * * * * * * *
*
*
*
* * * * * * * * *
private static void npattern1() {
for (int row = 1; row <= 9; row++) {
for (int col = 1; col <= 9; col++) {
if (row == 9 || col == 5 || col + row == 6 && row <= 4)
system.out.print("* ");
else
system.out.print(" ");
}
system.out.println();
}
}
}
output:
*
* *
* *
* *
*
*
*
*
* * * * * * * * *
星数和数字模式:>
package B1;
public class NSpattern {
public static void main(String[] args) {
pattern();
// pattern1();
// pattern2();
// pattern3();
// pattern4();
// pattern5();
// pattern6();
}
private static void pattern() {
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= row; col++)
System.out.print("* ");
System.out.println();
}
}
output:
*
* *
* * *
* * * *
* * * * *
private static void pattern6() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print(row + col + " ");
System.out.println();
}
}
output:
6 7 8 9 10
5 6 7 8
4 5 6
3 4
2
private static void pattern5() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print(row / col + " ");
System.out.println();
}
}
output:
5 2 1 1 1
4 2 1 1
3 1 1
2 1
1
private static void pattern4() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print(row * col + " ");
System.out.println();
}
}
output:
5 10 15 20 25
4 8 12 16
3 6 9
2 4
1
private static void pattern3() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print(col + " ");
System.out.println();
}
}
output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
private static void pattern2() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print(row + " ");
System.out.println();
}
}
output:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
private static void pattern1() {
for (int row = 5; row >= 1; row--) {
for (int col = 1; col <= row; col++)
System.out.print("* ");
System.out.println();
}
}
}
output:
* * * * *
* * * *
* * *
* *
*
以上就是用于循环编号模式:的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号