使用Java中的循环的数字模式

我一直在尝试for循环的不同变体,并且不知道如何制作这些模式:

模式1

54321 5432 543 54 5 

模式2

  1 12 123 1234 12345 

模式3

  12345 2345 345 45 5 

模式4

  1 123 12345 123 1 

几乎与模式1匹配的代码如下,但不像上面的例子那样工作。

 for (int i = 1 ; i  0 ; j-- ) { System.out.print(j); } System.out.print("\n"); } 

 public class PrintPattern { public static void main(String[] args){ printPattern1(); printPattern2(); printPattern3(); printPattern4(); } public static void printPattern1(){ for (int i = 0; i<5; i++){ for(int j = 5; j>i; j--) System.out.print(j); System.out.println(); } } public static void printPattern2(){ for (int i = 0; i<5; i++){ for(int k = 0; k<4-i; k++) System.out.print(" "); for(int j = 1; j<=i+1; j++) System.out.print(j); System.out.println(); } } public static void printPattern3(){ for (int i = 0; i<5; i++){ for(int k = 0; k 

}

你的内心循环

 for (int j = (rows + 1 - i) ; j > 0 ; j-- ){ System.out.print(j); } 

总是会倒数到1,因为它一直持续到j为零。 此外,当前行开始的数量取决于当前行,因为您在j的赋值中使用了i。 要获得模式1,这两件事都必须改变。

既然你发布了模式一的尝试,我会告诉你一个模式的解决方案 –

 int rows = 5; // <- Start at 5. for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s). for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again) System.out.print(j); } System.out.println(); } 

在你的问题中输出是模式1,

 54321 5432 543 54 5 

尝试这个:

 public class NumberPattern { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int k = 2; k >= i; k--) { System.out.print(" "); } for (int j = 1; j <= (2 * i - 1); j++) { System.out.print(j); } System.out.println(); } for (int i = 1; i <= 2; i++) { for (int k = i; k > 0; k--) { System.out.print(" "); } if (i % 2 != 0) { for (int j = 1; j <= (2 * i + 1); j++) { System.out.print(j); } } else { for (int j = 1; j <= (i / 2); j++) { System.out.print(j); } } System.out.println(); } } 

}

 public static void main(String[] args) { int rows = 5; System.out.println("------ PATTERN 1 ------"); for (int i = 1 ; i <= rows ; i++){ for (int j = rows; j >= i ; j--){ System.out.print(j); } System.out.println(); } System.out.println("\n------ PATTERN 2 ------"); for (int i = 1 ; i <= rows ; i++){ int k; for (k = rows ; k > i; k--){ System.out.print(" "); } for (int j = 1; j <= k ; j++){ System.out.print(j); } System.out.println(); } System.out.println("\n------ PATTERN 3 ------"); for (int i = rows ; i >= 1 ; i--){ int k; for (k = rows ; k > i; k--){ System.out.print(" "); } for (int j = 1; j <= k ; j++){ System.out.print(j); } System.out.println(); } System.out.println("\n------ PATTERN 4 ------"); int whitespaces = rows/2; for (int i = 1 ; i <= rows; i++){ // absolute value of whitespaces int abs_whitespaces = (whitespaces < 0 ? -whitespaces : whitespaces); for (int j = 0 ; j < abs_whitespaces ; j++){ System.out.print(" "); } for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){ System.out.print(j); } whitespaces-=1; System.out.println(); } } 

第一个项目是:

 class timepass { public static void main() { for (int a = -1;a<=5;a++) { for(int b = 5; b >= a;b--) { System.out.print("*"); } System.out.println(); // enter code here } } } 

第二个方案是:

 class timepass { public static void main() { for(int i = 1;i<= 6;i++) { for(int j = 1;j<= i ;j++) { System.out.print("*"); } System.out.println(); } } } 
 import java.util.Scanner; public class Triangle { public static void main(String[ ] args){ Scanner scan = new Scanner(System.in); System.out.println("enter no.of line you need"); int n = scan.nextInt(); for(int i=1;i<=n;i++){ for(int j=5;j>=i;j--){ System.out.print(j); } System.out.println(" "); }}}