编写一个 Java 程序,使用 for 循环左移奇数平方图案。
package Shapes4;
import java.util.Scanner;
public class SquareLeftShiftOddNum1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square Left Shift Odd Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Left Shift the Square Pattern of Odd Numbers");
for (int i = 1; i <= rows; i++)
{
for (int j = i - 1; j < rows; j++ )
{
System.out.print(j * 2 + 1 + " ");
}
for(int k = 0; k < i - 1; k++)
{
System.out.print(k * 2 + 1 + " ");
}
System.out.println();
}
}
}

这是另一种编写 Java 程序的方法,用于从上到下左移打印奇数平方图案。
package Shapes4;
import java.util.Scanner;
public class SquareLeftShiftOddNum2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square Left Shift Odd Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Left Shift the Square Pattern of Odd Numbers");
for (int i = 1; i <= rows; i++)
{
int j = (i * 2) - 1;
for(int k = 1; k <= rows; k++)
{
System.out.print(j + " ");
j += 2;
if(j > (rows * 2) - 1)
{
j = 1;
}
}
System.out.println();
}
}
}
Enter Square Left Shift Odd Numbers Rows = 13
Left Shift the Square Pattern of Odd Numbers
1 3 5 7 9 11 13 15 17 19 21 23 25
3 5 7 9 11 13 15 17 19 21 23 25 1
5 7 9 11 13 15 17 19 21 23 25 1 3
7 9 11 13 15 17 19 21 23 25 1 3 5
9 11 13 15 17 19 21 23 25 1 3 5 7
11 13 15 17 19 21 23 25 1 3 5 7 9
13 15 17 19 21 23 25 1 3 5 7 9 11
15 17 19 21 23 25 1 3 5 7 9 11 13
17 19 21 23 25 1 3 5 7 9 11 13 15
19 21 23 25 1 3 5 7 9 11 13 15 17
21 23 25 1 3 5 7 9 11 13 15 17 19
23 25 1 3 5 7 9 11 13 15 17 19 21
25 1 3 5 7 9 11 13 15 17 19 21 23
这个 Java 示例 使用 while 循环显示左移的奇数平方图案。
package Shapes4;
import java.util.Scanner;
public class SquareLeftShiftOddNum3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square Left Shift Odd Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Left Shift the Square Pattern of Odd Numbers");
int i, j, k;
i = 1;
while(i <= rows)
{
j = i - 1;
while(j < rows)
{
System.out.print(j * 2 + 1 + " ");
j++;
}
k = 0;
while(k < i - 1)
{
System.out.print(k * 2 + 1 + " ");
k++;
}
System.out.println();
i++;
}
}
}
Enter Square Left Shift Odd Numbers Rows = 15
Left Shift the Square Pattern of Odd Numbers
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
3 5 7 9 11 13 15 17 19 21 23 25 27 29 1
5 7 9 11 13 15 17 19 21 23 25 27 29 1 3
7 9 11 13 15 17 19 21 23 25 27 29 1 3 5
9 11 13 15 17 19 21 23 25 27 29 1 3 5 7
11 13 15 17 19 21 23 25 27 29 1 3 5 7 9
13 15 17 19 21 23 25 27 29 1 3 5 7 9 11
15 17 19 21 23 25 27 29 1 3 5 7 9 11 13
17 19 21 23 25 27 29 1 3 5 7 9 11 13 15
19 21 23 25 27 29 1 3 5 7 9 11 13 15 17
21 23 25 27 29 1 3 5 7 9 11 13 15 17 19
23 25 27 29 1 3 5 7 9 11 13 15 17 19 21
25 27 29 1 3 5 7 9 11 13 15 17 19 21 23
27 29 1 3 5 7 9 11 13 15 17 19 21 23 25
29 1 3 5 7 9 11 13 15 17 19 21 23 25 27