java打印各种三角形(空心&实心,正&倒)

java打印各种三角形(空心&实心,正&倒)

1.等腰三角形

import javax.swing.plaf.nimbus.State;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印的三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n-i;j++){
                System.out.print(" ");
            }
            for(int j = 1;j <= 2*i-1;j++){
                System.out.print("*");
            }
            System.out.println(" ");

        }
    }

}




结果


输入你要打印的三角形的行数
4
   * 
  *** 
 ***** 
******* 

Process finished with exit code 0

每一行需要打印的*数量为2×行数-1
每一行符号前的空格数为义工的行数N-符号的个数
2.打印空心等腰三角形

import javax.swing.plaf.nimbus.State;
        import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印的三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n-1;i++){
            for(int j = 1;j <= n-i;j++){
                System.out.print(" ");
            }

                System.out.print("*");


            for(int j = 1;j <= 2*i-3;j++){
                System.out.print(" ");
            }
            if(i != 1)
            System.out.print("*");
            System.out.println(" ");

        }
        for(int k = 1;k<=n*2-1;k++){
            System.out.print("*");
        }
    }

}





结果


输入你要打印的三角形的行数
4
   * 
  * * 
 *   * 
*******
Process finished with exit code 0

注意第一行特殊

3.打印直角三角形

import javax.swing.plaf.nimbus.State;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= i;j++){
            ;
            }

            System.out.println(" ");

        }
    }

}

结果


输入你要打印三角形的行数
5
* 
** 
*** 
**** 
***** 

Process finished with exit code 0

4打印空直角三角形

import javax.swing.plaf.nimbus.State;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印的三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n-1;i++){
            System.out.print("*");
            for(int j = 1;j <=i-2 ;j++){
                System.out.print(" ");
            }
            if(i!= 1)
            System.out.println("*");
            else
                System.out.println(" ");
        }
        for (int k = 1;k<= n;k++)
            System.out.print("*");

    }

}





结果


输入你要打印的三角形的行数
6
* 
**
* *
*  *
*   *
******
Process finished with exit code 0

5反直角三角形

import javax.swing.plaf.nimbus.State;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印的三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n-i;j++){
                System.out.print(" ");
            }
            for(int j = 1;j <= i;j++){
                System.out.print("*");
            }
            System.out.println(" ");

        }
    }

}


结果


输入你要打印的三角形的行数
4
   * 
  ** 
 *** 
**** 

Process finished with exit code 0

6倒直角三角形

import javax.swing.plaf.nimbus.State;
import java.util.Scanner;
public class HelloWorld {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("输入你要打印的三角形的行数");
        int n = input.nextInt();
        for(int i = 1;i <= n;i++){
            for(int j = 0;j <= n-i;j++){
                System.out.print("*");
            }
            for(int j = 1;j <= i;j++){
                System.out.print(" ");
            }
            System.out.println(" ");

        }
    }

}


结果


输入你要打印的三角形的行数
4
****  
***   
**    
*     

Process finished with exit code 0

你可能感兴趣的:(学习)