用Java程序打印左减数的正方形图案

82 阅读1分钟

编写一个Java程序,使用for循环打印左减数的正方形图案。

package Shapes3.X

import java.util.Scanner;

public class SquareLeftDecNum1 {


private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Square of Left Dec Numbers Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Square of Decrement Numbers from Left Side");
	
	for (int i = rows; i >= 1; i-- ) 
	{
		for (int j = i; j < rows; j++ ) 
		{
			System.out.print(j + " ");
		}
		for(int k = rows - i; k < rows; k++) 
		{
			System.out.print(rows + " ");
		}
		System.out.println();
	}
}

image.png

这个Java程序使用while循环显示左手边递减数的正方形图案。

package Shapes3.X

import java.util.Scanner;

public class SquareLeftDecNum2 {

private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Square of Left Dec Numbers Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Square of Decrement Numbers from Left Side");
	int j, k, i = rows; 
	
	while(i >= 1) 
	{
		j = i; 
		while(j < rows) 
		{
			System.out.print(j + " ");
			j++;
		}
		
		k = rows - i;
		while(k < rows) 
		{
			System.out.print(rows + " ");
			k++;
		}
		
		System.out.println();
		i--;
	}
}
Enter Square of Left Dec Numbers Rows = 9
Square of Decrement Numbers from Left Side
9 9 9 9 9 9 9 9 9 
8 9 9 9 9 9 9 9 9 
7 8 9 9 9 9 9 9 9 
6 7 8 9 9 9 9 9 9 
5 6 7 8 9 9 9 9 9 
4 5 6 7 8 9 9 9 9 
3 4 5 6 7 8 9 9 9 
2 3 4 5 6 7 8 9 9 
1 2 3 4 5 6 7 8 9