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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM