Java 打印乘法数直角三角形程序

在这个Java模式程序中,我们展示了如何使用for循环、while循环和函数在每一行打印乘法数直角三角形模式。

下面的程序接受用户输入的行数,并使用嵌套的for循环来遍历行和列。接下来,程序将以直角三角形模式打印乘法数。

import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();

for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}
Enter Numbers of Rows = 13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169 

在此程序中,我们用while循环替换了for循环来遍历行和列,并以直角三角形形状打印乘法数。更多数字模式程序,请点击此处

import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;

System.out.print("Enter Numbers of Rows = ");
rows = sc.nextInt();

i = 1 ;
while ( i <= rows )
{
j = 1 ;
while ( j <= i )
{
System.out.printf("%d ", i * j);
j++;
}
System.out.println();
i++;
}
}
}
Enter Numbers of Rows = 12
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 

在这个Java模式程序中,我们创建了一个RightTriMultiplicationNums函数,用于在每一行打印乘法数的直角三角形。


import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Numbers of Rows = ");
int rows = sc.nextInt();

RightTriMultiplicationNums(rows);
}

public static void RightTriMultiplicationNums(int rows)
{
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}
Program to Print Right Triangle of Multiplication Numbers