import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
for (int i = 1; i <= (n + 1) / 2; i++) {
for (int j = 0; j < (n + 1) / 2 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
通过修改 MAX_NUM 为 5 7 9
public class Test1
{
public final int MAX_NUM = 5;
public static void main(String[] args)
{
for (int i=0;i<MAX_NUM ;i++ )
{
for (int j=0;j<=MAX_NUM -i ;j++ )//输出空格随着循环是递减的
{
System.out.print(" ");
}
for (int k=0;k<=i*2 ;k++ )//输出*号随着外循环是递增的,当k<=i时 只打印三角形的一半,首先k是从0开始的
{
System.out.print("*");
}
System.out.println();
}
}
}
package sample;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
System.out.println("请输入一个奇数:");
int odd = input.nextInt();
if (odd%2 == 0 || odd < 0){
System.out.println("输入非奇数,请重新输入!");
}else {
for (int i = 1; i <= (odd + 1) / 2; i++) {
for (int j = 0; j < (odd + 1) / 2 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("* ");
}
System.out.println();
}
return;
}
}
}
}