for循环打印等腰三角形、直角三角形、菱形

一、等腰三角形

 1 package s1;
 2 
 3 import java.util.Scanner;
 4 
 5 public class C31 {
 6 
 7     public static void main(String[] args) {
 8 
 9     /**
10      * @author fklin
11      * 
12      * 
13      * 
14      *              *
15      *             ***
16      *            *****
17      *           *******
18      *          *********
19      */
20 
21 Scanner sc = new Scanner(System.in);
22 System.out.println("请输入您要打印的等腰三角形边数(只能为整数)");
23 int n = sc.nextInt();
24 
25     
26         for(int x=1;x<=n;x++){       // 先确定行数    
27             for(int z=n-x;z>=0;z--){
28                 System.out.print(" ");    //每行的空白数量
29             }
30             
31             for(int y =1;y<=(2*x-1);y++){         //每行的星星个数
32                  System.out.print("*");
33             }
34             System.out.println();
35         }
36     
37     sc.close();
38         
39     }

二、直角三角形

 1 package s1;
 2 
 3 import java.util.Scanner;
 4 
 5 public class C31 {
 6 
 7     public static void main(String[] args) {
 8 
 9     /**
10      * @author fklin
11      * 
12      *            *
13      *            ***
14      *            *****
15      *            *******
16      *            *********
17      * 
18      */
19 
20 Scanner sc = new Scanner(System.in);
21 System.out.println("请输入您要打印的直角三角形的行数(只能为整数)");
22 int n = sc.nextInt();
23 
24     
25         for(int x=1;x<=n;x++){       // 先确定行数                
26             for(int y =1;y<=(2*x-1);y++){         //每行的星星个数
27                  System.out.print("*");
28             }
29             System.out.println();
30         }
31     
32     sc.close();
33         
34     }
35 
36 }

 三、菱形

package s1;

import java.util.Scanner;

public class C31 {

    public static void main(String[] args) {

    /**
     * @author fklin
     * 
     * 
     *          *
     *         ***
     *        *****
     *       *******
     *      *********
     *     ***********
     *      *********
     *       *******
     *        *****
     *         ***
     *          *
     */

Scanner sc = new Scanner(System.in);
System.out.println("请输入您要打印的菱形的高度(只能为整数)");
int n = sc.nextInt();

    
//首先把菱形看成上下,上n行下n-1行,
//先打印出上面的等腰三角形
            for(int i=1;i<=n;i++)
            {
            //将空格和*分开看,看" "的变化i=1时,他是4 ,2的时候是3找规律
                 for(int j=1;j<=n-i;j++)               
                     System.out.print(" ");
                 for(int k=1;k<=2*i-1;k++)//找规律,i是 1 3 5 7 基数嘛
                   System.out.print('*');
                 //换一行
                 System.out.println();
             }
            
          
//打印下半部分
              for(int i=1;i<=n;i++)
            {
                 for(int j=1;j<=i;j++)//空格 1 2 3 4 so
                   System.out.print(" ");
                 for(int k=2*n;k>2*i+1;k--)//* 7 5 3 1倒着来的基数
                   System.out.print('*');
                   System.out.println();
             }
        
        
    
    sc.close();
            
    }

}

AnyConnect使用说明(电脑版Windows):

http://www.cnblogs.com/fklin/p/8652072.html

原文地址:https://www.cnblogs.com/fklin/p/7039547.html