编写一个Java程序,使用for循环打印一个方块图案,其中对角线上的数字除外,其余均为零。
package Shapes3;
import java.util.Scanner;
public class SqaurewithDiagNum1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square with Diagonal Numbers Side = ");
int rows = sc.nextInt();
System.out.println("Square with Numbers in Diaginal and Remaining 0's");
for (int i = 1; i <= rows; i++ )
{
for (int j = 1; j < i; j++ )
{
System.out.print("0 ");
}
System.out.print(i + " ");
for(int k = i; k < rows; k++)
{
System.out.print("0 ");
}
System.out.println();
}
}
}

这是在Java中用另一种方式打印具有对角线数字且其余项均为零的方块图案。
package Shapes3;
import java.util.Scanner;
public class SqaurewithDiagNum2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square with Diagonal Numbers Side = ");
int rows = sc.nextInt();
System.out.println("Square with Numbers in Diaginal and Remaining 0's");
for (int i = 1; i <= rows; i++ )
{
for (int j = 1; j <= rows; j++ )
{
if(i == j)
{
System.out.print(i + " ");
}
else
{
System.out.print("0 ");
}
}
System.out.println();
}
}
}
Enter Square with Diagonal Numbers Side = 9
Square with Numbers in Diaginal and Remaining 0's
1 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0
0 0 0 0 5 0 0 0 0
0 0 0 0 0 6 0 0 0
0 0 0 0 0 0 7 0 0
0 0 0 0 0 0 0 8 0
0 0 0 0 0 0 0 0 9
Java使用while循环打印带对角线数字的方块图案的程序
package Shapes3;
import java.util.Scanner;
public class SqaurewithDiagNum3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square with Diagonal Numbers Side = ");
int rows = sc.nextInt();
System.out.println("Square with Numbers in Diaginal and Remaining 0's");
int i = 1;
while( i <= rows)
{
int j = 1;
while(j <= rows)
{
if(i == j)
{
System.out.print(i + " ");
}
else
{
System.out.print("0 ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Square with Diagonal Numbers Side = 12
Square with Numbers in Diaginal and Remaining 0's
1 0 0 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0 0 0
0 0 0 0 0 6 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0 0 0
0 0 0 0 0 0 0 8 0 0 0 0
0 0 0 0 0 0 0 0 9 0 0 0
0 0 0 0 0 0 0 0 0 10 0 0
0 0 0 0 0 0 0 0 0 0 11 0
0 0 0 0 0 0 0 0 0 0 0 12
这个Java程序使用do while循环显示递增对角线数字的方块图案,其余数字均为零。
package Shapes3;
import java.util.Scanner;
public class SqaurewithDiagNum4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Square with Diagonal Numbers Side = ");
int rows = sc.nextInt();
System.out.println("Square with Numbers in Diaginal and Remaining 0's");
int i = 1;
do
{
int j = 1;
do
{
if(i == j)
{
System.out.print(i + " ");
}
else
{
System.out.print("0 ");
}
} while(++j <= rows);
System.out.println();
} while(++i <= rows);
}
}
Enter Square with Diagonal Numbers Side = 15
Square with Numbers in Diaginal and Remaining 0's
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 8 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 9 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 12 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 13 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 14 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 15