java基礎 ---- 練習for循環


-----   使用for循環打印圖形

//打印矩形
public class Print {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

//打印等腰三角形
public class Pint {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5-i;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=2*i-1;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

打印數字三角形

 1 /*
 2  * 打印等腰三角形
 3  * */
 4 public class PrintSJX {
 5     public static void main(String[] args) {
 6         //外層循環,執行五次,每次輸出一行*
 7         for (int i = 1; i <= 5; i++) {
 8             for(int j=1;j<=5-i;j++){
 9                 System.out.print(" ");
10             }
11             //內層循環,執行五次,每次輸出一個*
12             for (int j = 1;j<=2*i-1;j++){
13                 System.out.print(i);
14             }
15             
16             System.out.println();
17         }
18     }
19 }

 

//打印數字菱形
public class Pint {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5-i;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=2*i-1;j++){
                System.out.print(5-i);
            }
            System.out.println();
        }
        for(int i=1;i<=4;i++){
            for(int j=1;j<=i;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=2*(5-i)-1;j++){
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

 

 

/**
 * 反平行四邊形
 * @author bdqn
 *
 */
public class PrintEx6 {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            for(int j=1;j<=i-1;j++){
                System.out.print(" ");
            }
            for(int j=1;j<=7;j++){
                if(j<=4 && j>=1 ||j==7){
                    System.out.print("#");
                }
                else{
                    System.out.print(".");
                }
            }
            System.out.println();
        }
    }
}

 

----  打印100 以內的素數

 

 1 import java.util.Scanner;
 2 
 3 
 4 public class Prime {
 5 
 6     public static void main(String[] args) {
 7     
 8         public static void main(String[] args) {
 9         int flag = 0;
10         for(int i=1;i<=100;i++){
11             for(int j=2;j<i;j++){
12                 
13                 if(i%j==0){
14                     flag =1;  //標志位,不是質數
15                     break;
16                 }
17             }
18             if(flag==0){
19                 System.out.print(i+"\t");
20             }
21             flag = 0; //標志位清空
22         }
23         System.out.println();
24         
25         //更簡單的方法:
26         for(int i=1;i<=100;i++){
27             for(int j=2;j<=Math.sqrt(i);j++){
28                 
29                 if(i%j==0){
30                     flag =1;  //標志位,不是質數
31                     break;
32                 }
33             }
34             if(flag==0){
35                 System.out.print(i+"\t");
36             }
37             flag = 0;
38         }
39     }
40 
41 }

 

---恢復內容結束---


免責聲明!

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



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