编写一个 Java 程序,使用 for 循环从右侧打印递增数字的平方图案。
package Shapes3;
import java.util.Scanner;
public class SquareRightincNum1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square of Right Increment Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Square of Increment Numbers from Right Side");
for (int i = 1; i <= rows; i++ )
{
for (int j = 1; j <= rows - i; j++ )
{
System.out.print(1 + " ");
}
for(int k = 1; k <= i; k++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}

此 Java 程序使用 while 循环从右侧打印递增数字的平方图案。
package Shapes3;
import java.util.Scanner;
public class SquareRightincNum2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square of Right Increment Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Square of Increment Numbers from Right Side");
int j, k, i = 1;
while( i <= rows)
{
j = 1;
while(j <= rows - i )
{
System.out.print(1 + " ");
j++;
}
k = 1;
while( k <= i)
{
System.out.print(i + " ");
k++;
}
System.out.println();
i++;
}
}
}
Enter Square of Right Increment Numbers Rows = 9
Square of Increment Numbers from Right Side
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 3 3 3
1 1 1 1 1 4 4 4 4
1 1 1 1 5 5 5 5 5
1 1 1 6 6 6 6 6 6
1 1 7 7 7 7 7 7 7
1 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
在此 Java 示例 中,我们创建了一个 squareIncNum 函数,该函数将从右侧显示递增数字的平方图案。
package Shapes3;
import java.util.Scanner;
public class SquareRightincNum3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square of Right Increment Numbers Rows = ");
int rows = sc.nextInt();
System.out.println("Square of Increment Numbers from Right Side");
squareIncNum(rows);
}
public static void squareIncNum(int rows)
{
for (int i = 1; i <= rows; i++ )
{
for (int j = 1; j <= rows - i; j++ )
{
System.out.print(1 + " ");
}
for(int k = 1; k <= i; k++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
Enter Square of Right Increment Numbers Rows = 5
Square of Increment Numbers from Right Side
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5