zoukankan      html  css  js  c++  java
  • 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+"	");
    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+"	");
    36             }
    37             flag = 0;
    38         }
    39     }
    40 
    41 }

    ---恢复内容结束---

  • 相关阅读:
    lock,Monitor,Mutex的区别
    byte[]数组和int之间的转换
    接口测试总结
    python接口自动化测试(七)unittest 生成测试报告
    python 接口自动化测试(六)使用unittest 批量用例管理
    python 接口自动化测试(五)其他-认证&代理&超时配置
    python 接口自动化测试(四)cookie&session
    接口自动化测试 (三)request.post
    python 接口自动化测试二(request.get)
    python接口自动化测试(c测试环境的准备)
  • 原文地址:https://www.cnblogs.com/obge/p/10707104.html
Copyright © 2011-2022 走看看