一、杨辉三角
/**
* 杨辉三角
* 000010000 第一行的1是固定的,需要手动进行输入;
* 000101000
* 001020100
* 010303010
* 104060401
* 解题思路:
* 从第二行开始
* 首位等于上一行的下一位,末位等于上一行对应位的上一位
* 中间进行上一行的间隔求和
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.print("请输入行数:");
int num = scanner.nextInt();
int[][] array = new int[num][2 * num - 1];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (i == 0) {
array[0][num - 1] = 1; // 定义的元素为1,其它的默认为0;
break; // 只有第一行是自己定义,设置好值之后结束i=0这次循环;
}
if (j - 1 < 0) { // 如果从第二行开始,第一个元素
array[i][j] = array[i - 1][j + 1]; // 将上一行的第二个元素赋值本行的第一个元素;
}
if ((j + 1) > (2 * num - 2)) { // 从第二行开始,最后一个元素
array[i][j] = array[i - 1][j - 1]; // 最后一个元素的值等于上一行倒数第二个元素的值;
} else {
int k;
if ((i - 1) < 0 || (j - 1) < 0) {
k = 0;
} else {
k = array[i - 1][j - 1];
}
array[i][j] = k + array[i - 1][j + 1];
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int a = array[i][j];
if (a == 0) {
System.out.print(" "); // 此处把0换成空格;
} else {
System.out.print(a);
}
}
System.out.println("\n");
}
}
}
运行结果:
二、左对齐*号输出
/**
* 金字塔输出1
*
**
***
****
*****
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
scanner.close();
}
}
运行结果:
三、右对齐*号输出
/**
* 金字塔输出2
*
**
***
****
*****
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
for (int j = i; j < num - 1; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
运行结果:
四、金字塔 * 输出
/**
* 金字塔输出3
*
* *
* * *
* * * *
* * * * *
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
for (int j = i; j < num - 1; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
scanner.close();
}
}
运行结果:
五、奇数 * 金字塔
/**
* 金字塔输出5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int num = scanner.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = i; j < num; j++) {
System.out.print(" ");
}
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print("* ");
}
System.out.println();
}
scanner.close();
}
}
运行结果:
六、偶数 * 号金字塔
/**
* 金字塔输出6
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数:");
int num = scanner.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = i; j < num; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i * 2 - 1; k++) {
System.out.print("* ");
}
System.out.println();
}
scanner.close();
}
}
运行结果:
七、九九乘法表
/**
* 九九乘法表
*/
public static void main(String[] args) {
for (int i = 1; i <= 9;i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " * " + j + " = " + j * i + "\t");
}
System.out.println();
}
}
运行结果: